iterator over a numarray?

A

Alex Hunsley

I'm looking for a way to iterate over all the items in a numarray.
Writing a few nested loops isn't going to cut it, because the numarray
in question could be of any dimension...
I am aware of the revel function, but that appears to just flatten the
numarray. What I need is an iterator that can give each value and the
coordinates in the array of that item....

thanks
alex
 
F

Fernando Perez

Alex said:
I'm looking for a way to iterate over all the items in a numarray.
Writing a few nested loops isn't going to cut it, because the numarray
in question could be of any dimension...
I am aware of the revel function, but that appears to just flatten the
numarray. What I need is an iterator that can give each value and the
coordinates in the array of that item....

I'd suggest you repost this (and you other numarray question) directly to the
numarray list (numpy-dicsussion). The response rate tends to be better for
numpy/numarray specific questions on that list, since the users of those libs
don't always follow c.l.py regularly (though most follow it on and off).

Best,

f
 
C

Colin J. Williams

Alex said:
I'm looking for a way to iterate over all the items in a numarray.
Writing a few nested loops isn't going to cut it, because the numarray
in question could be of any dimension...
I am aware of the revel function, but that appears to just flatten the
numarray. What I need is an iterator that can give each value and the
coordinates in the array of that item....

thanks
alex

Alex,

You might try something like:
import numarray as _num
a= _num.arange(48, shape= (4, 4, 3))
b= iter()
c= b.next()
print `c`
d= b.next()
print `d`
This gives:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
array([[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]])
You can see that we move along the first dimension.

ravel() flattens an array in situ, flat delivers a flattened array.

Colin W.
 
A

Alex Hunsley

Colin said:
Alex said:
I'm looking for a way to iterate over all the items in a numarray.
Writing a few nested loops isn't going to cut it, because the numarray
in question could be of any dimension...
I am aware of the revel function, but that appears to just flatten the
numarray. What I need is an iterator that can give each value and the
coordinates in the array of that item....

thanks
alex


Alex,

You might try something like:
import numarray as _num
a= _num.arange(48, shape= (4, 4, 3))
b= iter()
c= b.next()
print `c`
d= b.next()
print `d`
This gives:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
array([[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]])
You can see that we move along the first dimension.

ravel() flattens an array in situ, flat delivers a flattened array.

Colin W.

Hi Colin and Fernado
thanks for your replies!
After a little hunting I found something that did the trick via google
groups.
The article in question is here:

http://makeashorterlink.com/?P69D22AB8

it details a way to generate a list of coordinate and value pairs, e.g.
values from a 2d numarray would then be represented as:

(((0,0), 2), ((1,0), 65), ((2,0), 33), ....etc)

thanks!
alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,483
Members
48,146
Latest member
Dino69J579

Latest Threads

Top