简体   繁体   English

使用二维数组的排序索引对 3D 数组进行排序

[英]Sort 3D array using sort indices of a 2D array

I have 2D and a 3D numpy array.我有 2D 和 3D numpy 阵列。 The 2d array A has shape (N, 3) and the 3d array B has shape (N, 3, 3) .二维阵列A具有形状(N, 3) ,3d 阵列B具有形状(N, 3, 3) I want to sort A along axis=1 and then apply that same sorting to array B sorting along axis=2 .我想A沿axis=1排序,然后将相同的排序应用于数组B沿axis=2排序。

I know I can do我知道我能做到

sort_idxs = np.argsort(A, axis=1)

but then I don't know how to apply sort_idxs in the way I need to array B .但后来我不知道如何以我需要排列B的方式应用sort_idxs sort_idxs has a shape of (N, 3) like A . sort_idxs的形状为(N, 3)A Somehow I need to map the first dimension of sort_idxs to the first dimension of B , map the second dimension of sort_idxs to the 3rd dimension of B , and ignore the second dimension of B .不知何故,我需要将 sort_idxs 的第一维sort_idxsB的第一维, map 将sort_idxs的第二维到B的第三维,并忽略B的第二维。 How can I do this?我怎样才能做到这一点?

This can be solved using这可以使用解决

sort_idxs = np.argsort(A, axis=1)
B_sorted = np.take_along_axis(B, sort_idxs[:, np.newaxis, :], axis=2)

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

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