[Numpy-discussion] slicing an n-dimensional array

Stefan van der Walt stefan at sun.ac.za
Thu Dec 4 04:23:37 EST 2014

On 2014-12-04 03:41:35, Jaime Fernández del Río <jaime.frio at gmail.com> wrote:
> nx = np.arange(A.shape[0])[:, np.newaxis]
> ny = np.arange(A.shape[1])
> C = A[nx, ny, B]

That's the correct answer--in my answer I essentially wrote

C = A[B] (== A[B, :, :])

which broadcasts the shape of B against the second and third dimensions
of A (it's almost always a bad idea to combine index broadcasting
and slicing).

The notes I linked to are correct, though, and explain Jamie's answer in
more detail (search for "Jack's Dilemma").


More information about the NumPy-Discussion mailing list