假设两个数组
ind =
array([[1,3,2,4,0],[0,1,4],[3,1]])
x =
array([[[24,97,28,57,59],[97,67,94,77,50],[56,89,25,55,76],[88,21,50,24]],[[54,83,64,81,12],[89,49,15,26,97],[94,32,79],[24,63,40]],[[41,99,84,21],[12,9,85,43,28],[75,98,48,10,[93,37,22,63]]])
我想根据第一个数组重新排序第二个数组(第一个数组是索引)
因此,结果可能如下所示.
array([[[97,59,24],[67,76,56],[21,24,88]],[[64,41,99],[43,12,9],[10,75,98],[22,93,94]]])
# x[0]s are reordered by ind[0] and so on.
np.take有可能吗?
最佳答案
使用take_along_axis很容易:
原文链接:https://www.f2er.com/python/533283.html>>> np.take_along_axis(x,ind[:,None,:],2)
array([[[97,94]]])
如果您使用的是1.15 numpy之前的版本,则可以执行以下操作:
>>> m,n,k = x.shape
>>> m,k = np.ogrid[:m,:n,:k]
>>> x[m,:]]
array([[[97,94]]])