I'd like to form a piece of code that gives a print statement if there is a detection of a nonzero number in an array. The attempt I have is as follows:
if numpy.where(array != 0):
print "NonZero element detected!"
elif numpy.where(array ==0):
print "All elements are zero"
I also know of the numpy.nonzero command but I'd really like to get this if, else style of printed statements working and I'm not sure how to incorporate the python logic properly. I'm more interested in getting the logic working than I am finding zeroes. What I have seems to produced the "NonZero element detected!" statement regardless of whether or not there are non-zeroes in the array. Any there any ideas on how to implement this?
wheregives you a tuple of arrays, one array per dimension. If there aren't any non-zeros those arrays will have shape (0,), i.e. no elements. So if you usewhereyou need to check the length of one of those arrays. I'd suggest experimenting withwherein an interactive session. Looking at its result should make this clearer.