Skip to content Skip to sidebar Skip to footer

How To Return The Positions Of The Maximum Value In An Array

As stated I want to return the positions of the maximum value of the array. For instance if I have the array: A = np.matrix([[1,2,3,33,99],[4,5,6,66,22],[7,8,9,99,11]]) np.argmax(

Solution 1:

Find the max value of the array and then use np.where.

>>> m = a.max()
>>> np.where(a.reshape(1,-1) == m)
(array([0, 0]), array([ 4, 13]))

After that, just index the second element of the tuple. Note that we have to reshape the array in order to get the indices that you are interested in.

Solution 2:

Since you mentioned that you're interested only in the last position of the maximum value, a slightly faster solution could be:

A.size - 1 - np.argmax(A.flat[::-1])

Here:

A.flat is a flat view of A.

A.flat[::-1] is a reversed view of that flat view.

np.argmax(A.flat[::-1]) returns the first occurrence of the maximum, in that reversed view.

Post a Comment for "How To Return The Positions Of The Maximum Value In An Array"