JaeWoo So
JaeWoo So

Reputation: 576

How can i find indices of element that bigger than special number from numpy 2d array?

I want to find index of element that bigger than 2 from numpy 2d array.

like this

import numpy as np
a = np.array([[1,2,3],[4,5,6]])

#  find indices of element that bigger than 2
# result  = [[0,2],[[1,0],[1,1],[1,2]]

Upvotes: 2

Views: 1275

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You can use np.where() which will gives you the expected indices in a tuple mode (separate axis):

In [6]: np.where(a>2)
Out[6]: (array([0, 1, 1, 1]), array([2, 0, 1, 2]))

Or directly the np.argwhere():

In [5]: np.argwhere(a>2)
Out[5]: 
array([[0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])

Upvotes: 3

Related Questions