0

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]
user20032724
  • 153
  • 5
  • Does this answer your question? [numpy get index where value is true](https://stackoverflow.com/questions/16094563/numpy-get-index-where-value-is-true) – DarrylG Dec 11 '22 at 10:41

2 Answers2

1

Try np.flatnonzero: np.flatnonzero(T<5)

mathfux
  • 5,759
  • 1
  • 14
  • 34
0

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
Gameplay
  • 1,142
  • 1
  • 4
  • 16