Indexing a numpy array with a numpy array of indexes [duplicate]
You can split pos
into 3 separate arrays and index, like so—
>>> i, j, k = pos.T>>> data[i, j, k]array([6, 2, 4])
Here, the number of columns in pos
correspond to the depth of data
. As long as you're dealing with 3D matrices, getting i
, j
, and k
well never get more complicated than this.
On python-3.6+, you can shorten this to—
>>> data[[*pos.T]]array([6, 2, 4])