[Numpy-discussion] maximum value and corresponding index

Robert Kern robert.kern@gmail....
Thu Aug 6 10:27:51 CDT 2009

2009/8/6 Hans Meine <meine@informatik.uni-hamburg.de>:
> On Wednesday 05 August 2009 22:06:03 David Goldsmith wrote:
>> But you can "cheat" and put them on one line (if that's all you're after):
>> >>> x = np.array([1, 2, 3])
>> >>> maxi = x.argmax(); maxv = x[maxi]
> Is there any reason not to put this as a convenience function into numpy?
> It is needed so frequently, and it's a shame that the shortest solution
> traverses the array twice (the above is usually longer due to variable names,
> and/or >1 dimensions).

The array is only traversed once. Indexing is O(1).

I'm -1 on adding a function to do this. If you want to keep that
convenience function in your own utilities, great.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list