Sort Data by Second Column

조회 수: 67 (최근 30일)
Brian Robinson
Brian Robinson 2020년 5월 26일
답변: Brian Robinson 2020년 5월 26일
Hello there,
I have data witht the first column being the year and the second column being the rainfall. I would like to sort the data from largest rainfall to smallest while keeping the associated years the same. I have tried using the Sort function of exactly how to do this.
Any help is appreciated,
Thanks,
Brian

채택된 답변

Stephen23
Stephen23 2020년 5월 26일
편집: Stephen23 2020년 5월 26일
Use sortrows, e.g. where M is your matrix:
M = sortrows(M,2);
If you really want to use sort, then of course you will need to use indexing, e.g.:
[~,idx] = sort(M(:,2));
M = M(idx,:);

추가 답변 (1개)

Brian Robinson
Brian Robinson 2020년 5월 26일
Thanks Stephen.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by