8

I have an array of numbers, and the maximum value might occurrence more than once.

How can we get a collection of indices of all the occurrences of the maximum value in the array?

For example, for the following array:

import numpy as np

a = np.array((1,2,3,2,3,2,1,3))

the result should be [2, 4, 7] (or an equivalent array or tuple).

1 Answer 1

11
import numpy as np

a = np.array((1,2,3,2,3,2,1,3))

occurences = np.where(a == a.max())

# occurences == array([2, 4, 7])
1
  • 6
    I'm not sure if this is true with more recent versions, but with v1.4.0, should have occurences = np.where(a == a.max())[0] such that you get an array rather than a tuple in occurences.
    – dtlussier
    Aug 15, 2011 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.