Is there any built-in function in Python3/Numpy which filters an array and returns indices of the elements which are left? Something similar to numpy.argsort for sorting. The filter I have is setting both min and max thresholds – all values below/above min/max have to be filtered out.
I’ve seen Python’s function filter, but I don’t see a way to extract indices using it.
EDITED: Lots of useful information in the answers, thank you!
As @SvenMarnach pointed out, mask is enough:
mask = (min_value < a) & (a < max_value)
Now I have to apply this mask to other arrays of the same shape as a, but not sure what is the best way to do it…
You can get the indices of the elements in the one-dimensional array
athat are greater thanmin_valueand les thanmax_valuewithUsually you don’t need those indices, though, but you can work more efficiently with the mask
This mask is an Boolean array with the same shape as
a.Edit: If you have an array
bof the same shape asa, you can extract the elements ofbcorresponding to theTrueentries inmaskwith