Reputation: 189
Let's say I have two vectors of size n containing coordinates (point1 and point2), and some large Numpy array with n dimensions (len(array.shape) == 3).
Now, all values of point1 are smaller than point2 and I want to extract the subarray contained between point1 and point2. If I knew the number of dimensions n beforehand (e.g. n=3), I would access it like this:
array[point1[0]:point2[0], point1[1]:point2[1], point1[2]:point2[2]]
I was wondering if there was a clean pythonic way to do this in Numpy that would work for any number of dimensions?
Thanks!
Upvotes: 1
Views: 59
Reputation: 2028
array[map(slice,point1,point2)]
The index of A[0:2,0:2]
is the same as (slice(0,2), slice(0,2))
which is a tuple of slice.
Upvotes: 2