Borrar filtros
Borrar filtros

Sort Data by Second Column

38 visualizaciones (últimos 30 días)
Brian Robinson
Brian Robinson el 26 de Mayo de 2020
Respondida: Brian Robinson el 26 de Mayo de 2020
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

Respuesta aceptada

Stephen23
Stephen23 el 26 de Mayo de 2020
Editada: Stephen23 el 26 de Mayo de 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,:);

Más respuestas (1)

Brian Robinson
Brian Robinson el 26 de Mayo de 2020
Thanks Stephen.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by