繁体   English   中英

如何通过另一个 2d 索引数组对 2d numpy-array 的每一行进行排序?

[英]How can I sort each row of the 2d numpy-array by another 2d index-array?

例如,我有:

arr = np.array[[10 30 20],
               [30 20 10]]

indices = np.array[[2 1 0],
                   [2 1 0]]

我想要:

[[20 30 10],
 [10 20 30]]

非常感谢!!

使用np.take_along_axis

import numpy as np

arr = np.array([[10, 30, 20],
               [30, 20, 10]])

indices = np.array([[2, 1, 0],
                   [2, 1, 0]])


res = np.take_along_axis(arr, indices, axis=1)
print(res)

输出

[[20 30 10]
 [10 20 30]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM