0

How do I index a lower dimensional data array with a higher dimensional index array?

E.g.: Given a 1d data array and a 2d index array:

data = np.array([11,12,13])
idx = np.array([[0,1],
                [1,2])

I would like to get a 2d data array:

np.array([[11,12],
          [12,13]])
1
  • What have you tried so far? Commented Sep 20, 2018 at 14:45

1 Answer 1

2

This is very easy in Python / NumPy, thanks to the advanced Numpy indexing system, you just use your indexing as the slicing, e.g. data[idx].

data = np.array([11,12,13])
idx = np.array([[0,1],
                [1,2]])

# this will produce the correct result
data[idx]
# array([[11, 12],
#        [12, 13]])
Sign up to request clarification or add additional context in comments.

2 Comments

i had tried this, but now realize that what was missing was ensuring that the index array has to be int32.
I used this to assign a 1D array with 2D array index... It's works too., but I don't know why...

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.