Sorting elements in a matrix via first column characterization

4 visualizaciones (últimos 30 días)
Adam Glick
Adam Glick el 15 de Jun. de 2015
Comentada: Stephen23 el 15 de Jun. de 2015
I'd like to know if there's a way to sort rows of a matrix by the entries in the first column.
For example, if I have a matrix
A = [9 4 5; 3 3 1; 5 8 4;]
I want to do something like "B = sort(A)" and have MATLAB return:
B = [3 3 1; 5 8 4; 9 4 5;]
Where all rows are sorted based on the first entry. Thanks.

Respuestas (1)

Andrei Bobrov
Andrei Bobrov el 15 de Jun. de 2015
Editada: Andrei Bobrov el 15 de Jun. de 2015
B = sortrows(A,1);
or
[~,ii] = sort(A(:,1));
B = A(ii,:);

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by