[Numpy-discussion] slicing an n-dimensional array

Benjamin Root ben.root at ou.edu
Wed Dec 3 20:34:27 EST 2014


Posting in the correct thread now...

A slightly different way to look at it (I don't think it is exactly the
same problem, but the description reminded me of it):
http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html
(and I think there are some things that can be done to make that faster,
but I don't recall it right now)

Ben Root

On Wed, Dec 3, 2014 at 7:02 PM, Stefan van der Walt <stefan at sun.ac.za>
wrote:

> Hi Catherine
>
> On 2014-12-04 01:12:30, Moroney, Catherine M (398E) <
> Catherine.M.Moroney at jpl.nasa.gov> wrote:
> > I have an array "A" of shape (NX, NY, NZ), and then I have a second
> array "B" of shape (NX, NY)
> > that ranges from 0 to NZ in value.
> >
> > I want to create a third array "C" of shape (NX, NY) that holds the
> > "B"-th slice for each (NX, NY)
>
> Those two arrays can broadcast if you expand the dimensions of B:
>
> A: (NX, NY, NZ)
> B: (NX, NY, 1)
>
> Your result would be
>
> B = B[..., np.newaxis]  # now shape (NX, NY, 1)
> C = A[B]
>
> For more information on this type of broadcasting manipulation, see
>
>
> http://nbviewer.ipython.org/github/stefanv/teaching/blob/master/2014_assp_split_numpy/numpy_advanced.ipynb
>
> and
>
> http://wiki.scipy.org/EricsBroadcastingDoc
>
> Stéfan
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141203/82c82635/attachment.html>


More information about the NumPy-Discussion mailing list