[Numpy-discussion] Getting 3-D array values from a 2-D array of indexes

Pau Gargallo pau.gargallo at gmail.com
Tue Jan 9 04:37:52 EST 2007

On 1/9/07, Jordan Dawe <jdawe at eos.ubc.ca> wrote:
> I have a 3D array A of shape (nz, ny, nx) and a 2D array B of shape (ny,
> nx) that contains integer indexes.  I want to generate a 2D array C of
> shape (ny, nx) composed of the values of A at the z-indexes specified in
> B.  Is there an easy way to do this?
> Jordan

you can use integer indexing if you first generate a complete set of
In your case you have the z-indexes, so you can generate the y and x
indices and then use integer indexing:

>>> y,x = indices((ny,nx))
>>> C = A[B,y,x]

hope it helps,

More information about the NumPy-Discussion mailing list