General Numerical Python question

Michael Ressler ressler at cheetah.jpl.nasa.gov
Fri Oct 17 12:20:11 EDT 2003


In article <500a4565.0310162140.1d7c9c1b at posting.google.com>, 2mc wrote:
> How would you handle the above if "a" were a 2d array since "nonzero"
> only works on 1d arrays?  Could you have used the "nonzero" function
> on a "vertical" slice of the array (from the perspective of an array
> of rows and columns - a vertical slice being the data in the column)?

I don't have a lot of experience with this yet, but every array has a
attribute called flat (e.g. a.flat) which is a 1-D representation of
the array. So if a is a 2-D (or 3-D) array, you could do something
like:

idx=nonzero(a.flat)
put(a.flat, values, idx)
print a

where a now has the appropriate values placed in their proper 2-D
positions.

As a side note, the numarray package (intended to be a Numeric
replacement) will provide better syntax for dealing with put and take,
maybe even handle 2-D issues like this transparently, but it's not
quite ready for prime time yet.

Mike

-- 
Dr. Michael Ressler
Research Scientist, Astrophysics Research Element, Jet Propulsion Laboratory
Email: ressler at cheetah.jpl.nasa.gov      Phone: (818)354-5576
"A bad night at the telescope is still better than the best day at the office."




More information about the Python-list mailing list