[Numpy-discussion] "Advanced indexing" question - subset of data cube as a 2D array.

Adam keflavich@gmail....
Wed Oct 29 00:44:06 CDT 2008

Hi numpy group,
    I have a problem I know there is an elegant solution to, but I can't
wrap my head around the right way to do the indexing.

The problem:
I have a 2D array that has been chopped up into 3 dimensions - it was [ time
X detectors ], it is now [ scans X time X detectors ].  During the chopping,
some of the time points and detector points have been removed, so the 3D
array contains only a subset of the data in the 2D array.  I'd like to
restore the 3D array back to the shape of the original 2D array b/c it's
being stored in a netCDF file that is not flexible.

My solution:
In [58]: array2d.shape
Out[58]: (11008, 144)

In [59]: array3d.shape
Out[59]: (23, 337, 107)

In [60]: whscan.shape
Out[60]: (7751,)

In [61]: 23*337
Out[61]: 7751

In [62]: temp2d = reshape(array3d,[23*337,107])

In [63]: temp2d2 = zeros([23*337,144])

In [64]: temp2d2[:,f.bolo_indices] = temp2d

In [65]: array2d[whscan,:] = temp2d2

This works, but it feels wrong to me: I think there should be a way to do
this by directly indexing array2d with two numpy arrays....

In the process of asking this question, I might have come up with the answer
(courtesy Stefan at http://mentat.za.net/):

In [85]: bi = (f.bolo_indices[np.newaxis,:]+ones([7751,1])).astype('int')

In [86]: whc = (whscan[:,np.newaxis] + ones([1,107])).astype('int')

In [87]: array2d[whc,bi] = temp2d

I thought this had worked, but the values didn't seem to be going to the
right places when I re-examined them.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081028/f3ca02ff/attachment.html 

More information about the Numpy-discussion mailing list