mardi 5 juillet 2016

pure numpy expression for selecting same-length subarrays with different starting indices from 3-D array

I have a 3-D numpy array (let's call it a) with shape (74, 74, 4563), and I want to extract a length-n sub-array from each location in the first two dimensions. However, each of those sub-arrays starts in a different place, depending on the indices in the first two dimensions, i & j. For example, if n=1000, I may want a[0, 0, 0:1000], but also a[0, 1, 2:1002], etc... I have a 2-d array (called ix0) which tells me where each sub-array starts for each i/j position. Finally, I am guaranteed that there will not be any "overflow"--that is, all the values in ix0 + n are smaller than the dimension-2 length of a (so we don't need to worry about asking for an index beyond the range that is present). I tried something like a[:, :, ix0:ix0 + n], but that raises IndexError: failed to coerce slice entry of type numpy.ndarray to integer. Is there a way to do this without looping through all the i/j index combinations or creating a big mask array?

Aucun commentaire:

Enregistrer un commentaire