[Numpy-discussion] 2D (or n-d) fancy indexing?

Zachary Pincus zachary.pincus@yale....
Wed Oct 8 20:00:16 CDT 2008

Hello all,

I'm doing something silly with images and am unable to figure out the  
right way to express this with "fancy indexing" -- or anything other  
than a brute for-loop for that matter.

The basic gist is that I have an array representing n images, of shape  
(n, x, y). I also have a "map" of shape (x, y), which contains indices  
in the range [0, n-1]. I then want to construct the "composite" image,  
of shape (x, y), with pixels selected from the n source images as per  
the indices in the map, i.e.:

composite[x, y] = images[map[x, y], x, y]
for all (x, y).

Now, I can't figure out if there's an easy way to express this in  
numpy. For that matter, I can't even figure out a simple way to do the  
1D version of the same:

composite[i] = images[map[i], i]
where composite and map have shape (m,), and images has shape (n, m).

Can anyone assist? Surely there's something simple that I'm just not  



More information about the Numpy-discussion mailing list