我正在尝试使用另一个数组indices
来索引一个多维数组P
。该数组指定我想要沿着最后一个轴选择哪个元素,如下所示:
import numpy as np
M, N = 20, 10
P = np.random.rand(M,N,2,9)
# index into the last dimension of P
indices = np.random.randint(0,9,size=(M,N))
# I'm after an array of shape (20,10,2)
# but this has shape (20, 10, 2, 20, 10)
P[...,indices].shape
如何使用 indices
正确地索引 P
,以获得形状为 (20,10,2)
的数组?
如果不太清楚:对于任何在范围内的 i
和 j
,我希望 my_output[i,j,:]
等于 P[i,j,:,indices[i,j]]
i
和j
,我希望my_output[i,j,:]
等于P[i,j,:,indices[i,j]]
。 - YXD