我想获得对应于特定索引的数组元素。所需输出已附加。
import numpy as np A=np.array([[1.1, 2.3, 1.9],[7.9,4.9,1.4],[2.5,8.9,2.3]]) Indices=np.array([[0,1],[1,2],[2,0]])
所需的输出是
array([[2.3],[1.4],[2.5]])
第一圈 Indices 获取行和列索引,即。 i0 和 i1 然后分别获取 A .用这个:
Indices
i0
i1
A
output = np.array([[A[i0][i1]] for i0, i1 in Indices])
输出:
array([[2.3], [1.4], [2.5]])
您可以使用 Indices 分别地第一列用于行索引,第二列用于列索引:
out = A[Indices[:, [0]], Indices[:, [1]]]