1

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!

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.