numpy: indexing 1d array with multidimensional index

479 views Asked by At

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

There are 1 answers

2
norok2 On BEST ANSWER

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]])