Sort Data by Second Column

45 次查看(过去 30 天)
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