How I can sort matrix according to both the first row and the third row as in the example?

时间:2015-07-08 15:53:30

标签: matlab sorting matrix

How I can sort M according to both the first row and the third row as in the following example:

M = [25 30 34 25 25 50 30 34
      3  4  8 11 99 58  3  4
     45 25 11 63 55 41 28 44]

the desired result:

Ms = [25 25 25 30 30 34 34 50
       3 99 11  4  3  8  4 58
      45 55 63 25 28 11 44 41]

1 个答案:

答案 0 :(得分:3)

You can use sortrows and just transpose twice (i.e. before and after) to effectively make it a "sortcolumns":

Ms = sortrows(M',[1,3])'