我正在尝试索引多维数组P
与另一个数组indices
。它指定我想要沿最后一个轴的哪个元素,如下所示:
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
我怎样才能正确索引P
with indices
获得形状数组(20,10,2)
?
如果这还不太清楚:对于任何i
and j
(在界限内)我想要my_output[i,j,:]
等于P[i,j,:,indices[i,j]]