sort a line of the matrix and change the other lines according the line sorted

조회 수: 3 (최근 30일)
Hello, i want to sort a line of the matrix and change the other lines according the line sorted. For example, i have a matrix like this:
a = [5 3 4 2 6; 1 2 3 4 5].
i want to sort line 1 and change line 2 according the sort of line 1, like this:
aSorted = [2 3 4 5 6; 4 2 3 5 1]
How can i do that??
Thank you for your help.

채택된 답변

Jon
Jon 2015년 8월 10일
편집: Jon 2015년 8월 10일
Use sortrows.
a = a'; % transpose your matrix
asorted = sortrows(a,1); % sort by first column (row)
asorted = asorted'; % de-transpose your matrix
You can combine all that code into one line:
asorted = sortrows(a')';

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 8월 10일
[~,ii] = sort(a(1,:));
out = 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