[SciPy-User] array indexing question

Nick C ncholy at gmail.com
Thu Jan 24 21:13:15 EST 2013


Ok, I think I found an improvement, myself; actually, it is a big 
improvement syntactically, and is perhaps more efficient:
B = A[I[np.newaxis, :], np.arange(Nj)[:, np.newaxis], 
np.arange(Nk)[np.newaxis, :]]

but is this the "right" way to do things?
-Nick


On Thursday, January 24, 2013 8:59:08 PM UTC-5, Nick C wrote:
>
> I'm fairly new to this, so apologies in advance.  
> Let's say I have a 3-d array A[i,j,k] of shape (Ni,Nj,Nk), and a list of 
> labels I[k], where I[k] belongs to 0...Ni-1.
> I want to create a new, 2-d array B[j,k] = A[I[k], j, k].
> I've found one solution to this, which is:
> ind_i = np.tile(I, (1,Nj)).T
> ind_j = np.tile(np.arange(Nj), (1,Nk))
> ind_k = np.tile(np.arange(Nk), (1, Nj)).T
> B = A[ind_i, ind_j, ind_k]
>
> but something seems...not ideal about this.  Can someone tell me if 
> there's a simpler, better way?
> Thanks,
> Nick
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130124/36a5b413/attachment.html>


More information about the SciPy-User mailing list