Sorting 2D matrix

조회 수: 29 (최근 30일)
Naga Alapati
Naga Alapati 2015년 11월 4일
댓글: dpb 2015년 11월 5일
Hello all,
I'm troubling with sorting of the matrix. I have a matrix
A = 6.0000 18.0000 20.0000 23.0000 26.0000
3.1464 1.4302 2.9512 0.9988 2.7403
I need to sort the second row in ascending order. Simultaneously the first row also needs to be sorted according to the second row like
required matrix = 23.0000 18.0000 26.0000 20.0000 6.0000
0.9988 1.4302 2.7403 2.9512 3.1464
when I trying to use the sort command like sort(A,2), I'm getting
sort(A,2)=
6.0000 18.0000 20.0000 23.0000 26.0000
0.9988 1.4302 2.7403 2.9512 3.1464
Can you help me here. Thank you.

채택된 답변

dpb
dpb 2015년 11월 4일
>> sortrows(A.',2).'
ans =
23.0000 18.0000 26.0000 20.0000 6.0000
0.9988 1.4302 2.7403 2.9512 3.1464
>>
  댓글 수: 2
Naga Alapati
Naga Alapati 2015년 11월 4일
Thank you. It works
dpb
dpb 2015년 11월 5일
Indeed! :) Converts the rows to columns since sortrows is limited to selecting columns by which to do the sorting.
The brute force way w/ sort would be two steps...
[~,ix]=sort(A(2,:)); % get the indices for sorting 2nd row
A=A(:,ix); % rearrange all by the column order

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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