我正在尝试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
以indices
获取形状数组(20,10,2)
?
如果这不是太清楚:对于任何i
和j
(在界限内)我想my_output[i,j,:]
等于P[i,j,:,indices[i,j]]