Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
I don't think it's that simple. There's an example below (it was too big to fit in this comment.) –  NLi10Me Nov 4 '14 at 0:26
    
Oh, wait, I haven't tried x as a tuple of arrays. I'll try that out. –  NLi10Me Nov 4 '14 at 0:26
    
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. –  NLi10Me Nov 4 '14 at 0:28
    
A list will work as well. –  wim Nov 4 '14 at 0:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.