我希望有人能帮助我。
我有一个5维的numpy数组:
my_array = {ndarray: (256,256,256,4,3,3)}
我想按最后一个维度(4)排序,让3 × 3块原封不动。换句话说,我想对很多3 × 3块排序,其中4个总是建立一个组。
举个小规模的例子,假设我有一个类似的数组
my_array = {ndarray: (256,256,256,4,2,2)}
对于256 * 256 * 256个组中的每个组,其可以如下所示:
[[[2,3],[1,3]],
[[1,2],[3,2]],
[[1,4],[2,1]],
[[1,2],[3,4]]]
我希望这些块像这样排序:
[[[1,2],[3,2]],
[[1,2],[3,4]],
[[1,4],[2,1]],
[[2,3],[1,3]]]
对于二维数组的简单情况,我可以通过使用my_2darray[:,np.lexsort(my_2darray)]
来实现这一点(对列排序并保持列完整
我试过使用np.sort(my_array, axis=3)
,它导致单个值被排序,而不是块,我试过my_array[:,np.lexsort(my_array)]
风格的所有变体和类似的,我发现没有什么工作。在一个sidenote上,我发现我想用lexsort排序的轴必须是最后一个,否则它的行为会很奇怪。没有问题,np.swapaxes,但是在高维的例子中仍然不能工作。有人有什么有用的见解吗?
谢谢大家!
1条答案
按热度按时间nzk0hqpo1#
技术上可以使用this solution,但应用到5维空间可能会有点复杂,所以这里是实现,请在使用前自己验证。