i hope someone can help me.
I have a numpy array with 5 dimensions:
my_array = {ndarray: (256,256,256,4,3,3)}
I want to sort it by the last dimension(4), leaving intact the 3x3 blocks. Put differently, i want to sort a lot of 3x3 blocks, where 4 of them always build a group.
On a small scale example, suppose i have a similar array
my_array = {ndarray: (256,256,256,4,2,2)}
for every group of the 256*256*256 groups that can look like this:
[[[2,3],[1,3]],
[[1,2],[3,2]],
[[1,4],[2,1]],
[[1,2],[3,4]]]
i want the blocks to be sorted like this:
[[[1,2],[3,2]],
[[1,2],[3,4]],
[[1,4],[2,1]],
[[2,3],[1,3]]]
For the simple case of a 2d array I was able to achieve this (sort columns and keeping columns intact) by using my_2darray[:,np.lexsort(my_2darray)]
I tried using np.sort(my_array, axis=3)
which led to the individual values being sorted, not the blocks, I tried all variations in the style of my_array[:,np.lexsort(my_array)]
and similar, and I find nothing that works. On a sidenote, I found out that the axis I want to sort by with lexsort needs to be last, otherwise it behaves weirdly. No problem, did np.swapaxes, but still couldn't make it work in the highdimensional example. Does someone have some helpful insight?
Thank you!