We have a matrix:
Index: 1 2 3 4 5 6
Elements:
0 2 0 5 9 8
1 0 5 0 5 4
0 1 0 0 6 0
Is it possible to be sorted by size of different elements per column?
Result should be like this:
Index: 5 2 6 1 3 4
Elements:
9 2 8 0 0 5
5 0 4 1 5 0
6 1 0 0 0 0
Thank you in advance.
With “length” and “unique” functions you can retrieve the number of different values of each column (including zero), for example:
If you want to ignore zeros you could use
Which gives an array with the number of different elements of each column:
Then you can sort this array and use the ordered indexes to sort your matrix: