Python Forum

Full Version: Multiple conditions when indexing an array with or without np.where
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I would appreciate some tips regarding my problem. There is an array (np.array) where I should find rows and columns of interesting elements. The array has values between 0 and 360

Example:

[20 20 33 360]
[54 62 27 89]
[120 200 300 54]

Now this works: r, c = np.where(array <20)

This does not: r, c = np.where(340 < array < 20) or np.where(array > 340 | array < 20 ) or np.where(array > 340 or array < 20 )

This also does not work: x = (array > 340) | array(array < 20)

Error I get is ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

What would be the best way to find rows and columns of elements for which multiple conditions have to be checked. Of course I could write r, c for every condition separately using np.where but that would be to many lines.

Any tips would be appreciated
Clarifying - you want the row and column indexes where the value meets a set of criteria? Or, is it dependent on other values as well (value is less than 20 but another in the row must be greater than 300 for example)?