Home > Blockchain >  How do you view the first non zero number in an NumPy array?
How do you view the first non zero number in an NumPy array?

Time:05-21

I have a cube a = numpy.array(n,n,n). The size will vary.

I want to look from the top, so down on the columns, which there are nxn of, and return an array (n,n) with the first non zero number you can 'see'.

This is like looking down on the array imagining zeros are holes.

I can use sum(a, axis=0) but this adds up any number below the first zero also. I want just the first number after a zero (or zero if there are n zeros in the column)

I hope this is clear enough to get some advice :)

Cheers, Paul

CodePudding user response:

from what I understand you can use non zero function with slicing. refer to : https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html

CodePudding user response:

You can find the element you searche with np.where(a==0), if there is more than one, you can find the first index with np.where(a==0)[0][0] so to get the first number after a zero you should use a[np.where(a==0)[0][0] 1]

  • Related