How do I maintain rows when sorting a matrix in MATLAB?

后端 未结 2 1497
感动是毒
感动是毒 2021-01-19 20:06

I have a 2-by-3 matrix, and I want to sort it according to the first column. Here\'s an example:

data   will change to -->  new data
11 33                         


        
相关标签:
2条回答
  • 2021-01-19 20:29

    As @gnovice suggests, sortrows is the best solution here. You can also specify more than one output for the sort and sortrowscommands, which will return the sort index. You can use this to modify your other columns as well or just to keep track of the permutation. For example:

    A=rand(10,2);
    [B, idx]=sortrows(A);
    
    0 讨论(0)
  • 2021-01-19 20:30

    The SORTROWS function can handle that for you:

    B = sortrows(A);
    
    0 讨论(0)
提交回复
热议问题