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

Chase, Holden hchase at ball.com
Wed Oct 18 10:32:07 CDT 2006



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.  





import numpy                #Could be numarray as well


def arrayMaxInd(data):

    xMax = data.argmax(1)

    maxVal = 0

    xMaxInd = 0

    yMaxInd = 0

    for yInd in range(data.shape[0]):

        if data[yInd][xMax[yInd]] > maxVal:

            maxVal = data[yInd][xMax[yInd]]

            yMaxInd = yInd

            xMaxInd = xMax[yInd] 

    return [xMaxInd, yMaxInd, maxVal]


# test the function

testdata = numpy.random.rand(4,6)

print arrayMaxInd(testdata)

print testdata


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20061018/6abbb48b/attachment.html 

More information about the SciPy-user mailing list