[Numpy-discussion] 3D array -> 2D array issue...

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Feb 7 07:37:01 EST 2011


On Mon, Feb 7, 2011 at 7:21 AM, Fred <fredmfp at gmail.com> wrote:
> Hi all,
>
> Let's say C1 is a 3D array,
> and q0 and k are 2D array.
>
> dim C1 = nx*ny*nz
>
> dim q0 = nx*ny = dim k
>
> I have to do the following:
>
> q0[0, 0] = C1[0, 0, k[0, 0]]
> q0[1, 1] = C1[1, 1, k[1, 1]]
> ...
> q0[i, j] = C1[i, j, k[i, j]]
> ...
>
> I tried
>
> q0 = C1[:, :, k]
>
> but this obviously does not work.

you need to build or broadcast the indices, something like this
(written, not tried out)

n0, n1 = k.shape
ind0 = np.arange(n0)[:,None]
ind1 = np.arange(n1)
q0 = C1[ind0,ind1, k[ind0,ind1]]

or better
q0 = C1[ind0,ind1, k]

Josef

>
>
> How could I do this ala NumPy?
>
> TIA
>
> Cheers,
>
> --
> Fred
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list