Cropping A Numpy Array Of An Arbitrary Dimension Given Two Corners
Solution 1:
The portion of the numpy indexing page that @Joe referred to is likely this:
Note Remember that a slicing tuple can always be constructed as obj and used in the
x[obj]
notation. Slice objects can be used in the construction in place of the[start:stop:step]
notation. For example,x[1:10:5,::-1]
can also be implemented as:obj = (slice(1,10,5), slice(None,None,-1))
x[obj]
This can be useful for constructing generic code that works on arrays of arbitrary dimension.
Using this concept you should be able to build your tuple of slices ahead of time then apply it to A
.
obj = tuple(slice(D1[i], D2[i]) for i inrange(D1.shape[0]))
A[obj]
Note* this is not actually using advanced indexing, as you are still providing a tuple of slice objects which is just the longhand / functional equivalent of using slices separated by colons and commas: A[d11:d21, ...
Advanced indexing utilizes arrays of different datatypes rather than exclusively tuples of slice
objects.
Post a Comment for "Cropping A Numpy Array Of An Arbitrary Dimension Given Two Corners"