MATLAB Answers

Sort Data by Second Column

3 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 26 May 2020
Edited: Stephen Cobeldick on 26 May 2020
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,:);

  0 Comments

Sign in to comment.

More Answers (1)

Brian Robinson
Brian Robinson on 26 May 2020
Thanks Stephen.

  0 Comments

Sign in to comment.

Tags


Translated by