I'm using np.where to find the indices of an array which satisfy a set of conditions. Is there an easy way to find the indices of all other elements which do not satisfy that condition? For e.g., if I use ok=np.where((condition1) & (condition2)) on array x I can only find x[ok]. How do I find instead all elements in x that don't satisfy ok?
CodePudding user response:
If you store the condition itself separately, you can just negate it:
true_indices = (condition1) & (condition2)
false_indices = ~true_indices
You can then call np.where on these values if you want.
CodePudding user response:
Welcome to Stackoverflow, Lily.
In python, the boolean negation operator is ~. It will give you the complement.
x = np.array([0, 1])
ok = np.where(x > 1)
not_ok = np.where(~(x > 1))
# you can also use this, if you already have ok
# where returns a tuple, the first index of which is the indices
not_ok = x[~ok[0]]
