[Numpy-discussion] Getting the indexes of the myarray.min()
Tim Hochberg
tim.hochberg at cox.net
Wed May 12 14:34:05 EDT 2004
Álvaro Tejero Cantero wrote:
>Hello,
>
>
>
>>To find the index of ONE minimum value is easy,
>>though it is buried in the nd_image sub-package
>>(where many users might miss it):
>>numarray.nd_image.minimum_position
>>
>>
>
>It works great... but what about efficiency? If I do times.min() and
>then numarray.nd_image.minimum_positioan(times) I am running twice
>essentially the same extremum-finding routine, which is prohibitibe for
>large N..., am I right?
>
>Which makes me thing of a more general question: I know that some of the
>array functions are coded in C for speed, but what about the classical
>python-for loop, as in (r Nx3 array of particle positions)
>
>[ [r[i]-r[j] for i in arange(N)] for j in arange(N)]
>
>is this handled to C code?
>
>
Try this:
>>> import numarray as na
>>> r = na.arange(5)
>>> na.subtract.outer(r, r)
array([[ 0, -1, -2, -3, -4],
[ 1, 0, -1, -2, -3],
[ 2, 1, 0, -1, -2],
[ 3, 2, 1, 0, -1],
[ 4, 3, 2, 1, 0]])
Look up the special methods on ufuncs (outer, reduce, etc) for more details.
>>I do not know a clean way to find all locations
>>of the minimum value. I hope somebody else does.
>>
>>
>
>Yes... although for the problem at hand that motivated my query, my
>times matrix is symmetric... I don't really need all the minima, but
>does numarray have any special datatype for symmetric matrixes, that
>prevents storage of unneded (e.g. supradiagonal) elements?.
>
>
Not that I know of.
-tim
>
>Thank you very much, I'm on my way to get some beautiful code out of old
>fortranisms
>
>á.
>
>
More information about the NumPy-Discussion
mailing list