[Numpy-discussion] is there a way to get Nd indices of max of Nd array

Stéfan van der Walt stefan at sun.ac.za
Sun May 3 21:00:45 EDT 2009


2009/5/3  <josef.pktd at gmail.com>:
>>>> factors = np.random.randint(5,size=(4,3))
>>>> factors
> array([[1, 1, 3],
>       [0, 2, 1],
>       [4, 4, 1],
>       [2, 2, 4]])
>>>> factors.max()
> 4
>>>> np.argmax(factors)
> 6
>>>> np.nonzero(factors==factors.max())
> (array([2, 2, 3]), array([0, 1, 2]))

Since you have more than one maximum here, you have to do something
like Josef outlined above.  If you only want the indices of the first
maximum argument, you can use

np.unravel_index(6, factors.shape)

Regards
Stéfan



More information about the NumPy-Discussion mailing list