[SciPy-user] More elegant way to get indices of the max value in an?array?

Steve Lianoglou lists.steve at arachnedesign.net
Wed Oct 18 12:24:28 EDT 2006


> On Wed, Oct 18, 2006 at 11:47:30AM -0400, Alan G Isaac wrote:
>> On Wed, 18 Oct 2006, Holden Chase apparently wrote:
>>> I would like to get the array indices of the max value in an  
>>> array.  Is
>>> there a more elegant way than what I have implemented below using
>>> argmax()?  This routine returns the indices and the max value.
>>
>>
>> Untested:
>> def arrayMaxInd(data):
>>         a = asarray(data)
>>         argmax1d = a.argmax()
>>         rs,cs = a.shape
>>         r,c = argmax1d//cs, argmax1d%cs
>>         return (r,c,argmax1d)
>
> How about numpy.unravel_index

If you're just looking for the indices of the max value in your  
array, why not just do something like:

---

import numpy as np

# ...
# let some_array be your array that you want to find max val for

maxval_indices = np.where(some_array == some_array.max())

---

That would do the trick, too .. no?

-steve




More information about the SciPy-User mailing list