[Numpy-discussion] Getting the indexes of the myarray.min()

Robert Kern rkern at ucsd.edu
Wed May 12 17:29:32 CDT 2004


Perry Greenfield wrote:

> Álvaro Tejero Cantero wrote:
> 
>>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?
>>
> 
> Well, yes. But when you ask to find all the things that equal
> the minimum, you pretty much must look twice (if you want to know
> where they all are if more than one). Once to determine the
> minimum, the next time to locate all of them.

Nah, you can accumulate indices corresponding to the current minimum 
value as you go. Discard the list of indices and start again if you get 
a new minimum value.

minval = data[0]  # let's suppose data is a vector for demonstration
minindices = [0]

for i in xrange(1, len(data)):
     x = data[i]
     if x < minval:
         minindices = [i]
         minval = x
     elif x == minval:
         minindices.append(i)

Whether or not this is faster (when implemented in C) than going over it 
twice using numarray functions is another question. My guess: not enough.

[snip]

>>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 for special cases like this. One could probably write a special
> subclass to do this, but for a savings of a factor of 2 in memory,
> it usually would not be worth the trouble (unlike sparse matrices)

OTOH, having subclasses that follow LAPACK's symmetric packed storage 
scheme would be very useful not because of the space factor but the time 
saved by being able to use the symmetric algorithms in LAPACK. I think.

> Perry

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Numpy-discussion mailing list