2

Suppose img is a 2 dimensional numpy array. Suppose also that x and y are integer valued 2 dimensional numpy arrays of the same shape as img. Consider:

newImg = img[x, y]

newImg is now a 2 dimensional array of the same shape as img where newImg[i,j] == img[ x[i,j], y[i,j] ] for all i and j.

I want to generalize this procedure to an arbitrary number of dimensions. That is, let img be a d-dimensional numpy array and take x[i], for i in range(0, d), to be an integer valued d-dimensional numpy array of the same shape as img. What I basically want is:

newImg = img[x[0], x[1], ..., x[d-1]]

Which is obviously pseudocode, and not expected to work.

How can I do this with NumPy?

0

1 Answer 1

1

Have you tried simply

newImg = img[x]

It looks like this should work!

I'm assuming x is a list or tuple of integer arrays satisfying the following conditions

len(x) == img.ndim
all(a.shape == img.shape for a in x)

which seems to match what you are describing.

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

3 Comments

I don't think it's that simple. There's an example below (it was too big to fit in this comment.)
Oh, wait, I haven't tried x as a tuple of arrays. I'll try that out.
Sorry, the tuple of arrays works. Note, that is the solution. In my question, I was assuming x was an array of arrays. The tuple works.

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.