# slicing an n-dimensional array Classic List Threaded 5 messages Open this post in threaded view
|

## slicing an n-dimensional array

 Hello, I'm sure there's a simple solution, but I'm not seeing it so any hints would be greatly appreciated. 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). For example: A = numpy.zeros((NX,NY,NZ)) A[:,:,0] = numpy.arange(0, NX*NY).reshape(NX,NY) A[:,:,1] = numpy.arange(1, NX*NY+1).reshape(NX,NY) and so on B = numpy.zeros((NX,NY)) B[0,0] = 0 B[0,1] = 1 so C[0,0] = A[0,0,B[0,0]]    C[0,1] = A[0,1,B[0,1]]    ...    C[NX-1,NY-1] = A[NX-1,NY-1,B[NX-1,NY-1]]    and C has shape(NX,NY) How do I accomplish this without loops? Thank-you for any advice, Catherine _______________________________________________ NumPy-Discussion mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: slicing an n-dimensional array

 Hi Catherine On 2014-12-04 01:12:30, Moroney, Catherine M (398E) <[hidden email]> 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.ipynband http://wiki.scipy.org/EricsBroadcastingDocStéfan _______________________________________________ NumPy-Discussion mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: slicing an n-dimensional array

 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 RootOn Wed, Dec 3, 2014 at 7:02 PM, Stefan van der Walt wrote:Hi Catherine On 2014-12-04 01:12:30, Moroney, Catherine M (398E) <[hidden email]> 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 [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/numpy-discussion