# [SciPy-user] How to use argmax ?

Daniel Lenski dlenski@gmail....
Mon Oct 22 13:57:17 CDT 2007

```On Mon, 22 Oct 2007 11:00:18 -0700, LB wrote:
> But what if a is 2D or more :
>>>> a = array([[10, 20, 14], [60, 5, 7]]) ind = a.argmax(axis=0)
>>>> a
> array([[10, 20, 14],
>        [60,  5,  7]])
>>>> ind
> array([1, 0, 0])
>>>> a[ind]
> array([[60,  5,  7],
>        [10, 20, 14],
>        [10, 20, 14]])
>>>> a.max(axis=0)
> array([60, 20, 14])
>
> How can I combine a and ind to access to the max ?

Hi, if I understand correctly, you're trying to get the indices in a of
the maxima.  Is that right?

Try this:
>>>> a = array([[10, 20, 14], [60, 5, 7]])
>>>> ind = (indices(a.shape)[0] == a.argmax(axis=0))
>>>> ind
array([[False,  True,  True],
[ True, False, False]], dtype=bool)

>>>> ind.nonzero()
(array([0, 0, 1]), array([1, 2, 0]))

This gives the locations of the maxima in a: a[0,1], a[0,2], and a[1,0].

The indices() function basically generates an array A, where:
A[axis,x,y,z...] = x if axis==0
y if axis==1
z if axis==2
etc.

Hope that helps.

Dan

```