Sorting elements in a matrix via first column characterization

조회 수: 4 (최근 30일)
Adam Glick
Adam Glick 2015년 6월 15일
댓글: Stephen23 2015년 6월 15일
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.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 6월 15일
편집: Andrei Bobrov 2015년 6월 15일
B = sortrows(A,1);
or
[~,ii] = sort(A(:,1));
B = A(ii,:);

카테고리

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