I have a numpy array that is one dimensional. I would like to get the biggest and the smallest index for which a property is true.
For instance,
A = np.array([0, 3, 2, 4, 3, 6, 1, 0])
and I would like to know the smallest index for which the value of A
is larger or equal to 4
.
I can do
i = 0
while A[i] < 4:
i += 1
print("smallest index", i)
i = -1
while A[i] <4:
i -= 1
print("largest index", len(A)+i)
Is there a better way of doing this?
As suggested in this answer,
np.argmax(A>=4)
returns 3
, which is indeed the smallest index. But this doesn't give me the largest index.