I have an array T
. I am printing all values which meet the criterion: T<5
. However, I don't know how to print the corresponding indices of values meeting this criterion. I present the expected output.
import numpy as np
T=np.array([4.7,5.1,2.9])
T1=T[T<5]
print([T1])
The expected output is
J=[0,2]
CodePudding user response:
Try np.flatnonzero
: np.flatnonzero(T<5)
CodePudding user response:
argwhere
does that for you
https://numpy.org/doc/stable/reference/generated/numpy.argwhere.html
x = np.arange(6).reshape(2,3)
array([[0, 1, 2],
[3, 4, 5]])
np.argwhere(x>1)
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])
The meaning of output is:
- row 0 element 2
- row 1 element 0
- row 1 element 1
- row 1 element 2