Loopless syntax for 2d in NumPy (or Numarray)
Terry Reedy
tjreedy at udel.edu
Sun Oct 19 18:06:56 EDT 2003
"2mc" <mcrider at bigfoot.com> wrote in message
news:500a4565.0310191108.55e52b1d at posting.google.com...
> Assume a multidimensional array (2d). This would be like a
> spreadsheet of rows and columns. Further, assume hundreds of 'rows'
> and 3 columns. Suppose I want a running list of the highest value in
a
> single column for 20 'rows'. So, starting at 'row' 19, the answer
> would be the highest value from 'row' 0 to 'row' 19. Then, at 'row'
> 20, the answer would be the highest value from 'row' 1 to 'row' 20.
> And, so on. Further, suppose I want this value for each 'column'.
> The result would be a 3 'column' array with 19 less rows than the
> source array and would contain a running list of highest values of
> each column for the last 20 rows.
>
> How would this be done without loops? Or, at least without looping
> through every row.
Just curious: is this a real problem, or one you made up to stump
NumPy? Keep in mind that NumPy was written to do typical array
operations, linear algebra/analysis, ffts, and even some things not so
typical. A moving maximum is highly nonlinear, unusual, and likely to
need explicit looping to be done efficiently.
I think the way to avoid redoing the max from scratch with each move
of the window is to use a heap augmented by a circular index that
enables easy replacement of the departing number with the incoming
number. After the replacement, re-establish the heap property and
record the max. (For more on heaps, see heapq.py in the lib or
various algorithm books.)
Terry J. Reedy
More information about the Python-list
mailing list