How can I sort a 2-D array in MATLAB with respect to one column?

前端 未结 2 1723
温柔的废话
温柔的废话 2020-11-27 05:33

I would like to sort a matrix according to a particular column. There is a sort function, but it sorts all columns independently.

For example, if my mat

2条回答
  •  鱼传尺愫
    2020-11-27 06:36

    An alternative to sortrows(), which can be applied to broader scenarios.

    1. save the sorting indices of the row/column you want to order by:

      [~,idx]=sort(data(:,1));
      
    2. reorder all the rows/columns according to the previous sorted indices

      data=data(idx,:)
      

提交回复
热议问题