1

I'm looking for the most idiomatic and performant way to slice an array of dimension N with an array of dimension N-1 that represents the index of the last dimension to slice. The output would have dimension N-1.

As an example:

import numpy as np

array = np.array([[[1,2], [3,4]], [[5,6], [7,8]]])
index = np.array([[0, 1], [1, 0]])

Results would be:

np.array([[1, 4], [6, 7]])

For example, one could imagine an array (lat, lon, alt) and we wish to extract a specific altitude for each (lat, lon). This information is stored in a 2D array.


I am looking for a fast solution as the arrays are quite large.

Thank you!

thomas
  • 51
  • 1
  • 5

0 Answers0