0

I have the following array. I want to extract indices by a search key of array. np.array([[1,2],[2,2], [3,2], [2,2]])'s shep is (4, 2). So the result of searching [2, 2] should return [1, 3] which is an index of [2, 2]. np.argwhere is a strong function, though, how to use this for my purpose?

In [31]: d=np.array([[1,2],[2,2], [3,2], [2,2]])    
In [32]: d
Out[32]:
array([[1, 2],
       [2, 2],
       [3, 2],
       [2, 2]])

In [33]: np.where(d==np.array([2,2]))
Out[33]: (array([0, 1, 1, 2, 3, 3]), array([1, 0, 1, 1, 0, 1]))
In [34]: np.argwhere(d==np.array([2,2]))
Out[34]:
array([[0, 1],
       [1, 0],
       [1, 1],
       [2, 1],
       [3, 0],
       [3, 1]])
jef
  • 3,890
  • 10
  • 42
  • 76

1 Answers1

0

Use np.all(..., axis=1) to reduce the boolean array over the second axis, then use np.where:

np.where((d == [2, 2]).all(1))[0]
# array([1, 3])
Psidom
  • 209,562
  • 33
  • 339
  • 356