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

josef.pktd at gmail.com josef.pktd at gmail.com
Sun May 3 16:34:23 EDT 2009


On Sun, May 3, 2009 at 3:30 PM, Chris Colbert <sccolbert at gmail.com> wrote:
> my case is only for 2d, but should apply to Nd as well.
>
> It would be convienent if np.max would return a tuple of the max value and
> its Nd location indices.
>
> Is there an easier way than just using the 1d flattened array max index
> (np.argmax) and calculating its corresponding Nd location?
>

>>> 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]))

Josef



More information about the NumPy-Discussion mailing list