I have a matrix which first row indicates index numbers and second indicated the data. For example it goes like this:
1 2 3 4 5 6
23 45 10 90 11 34
I want to sort these descending but I don't want to loose the corresponding index either.
4 2 6 1 5 3
90 45 34 23 11 10
I have a large amount of data so it needs to be efficient too. How can I do that?

 채택된 답변

Julia
Julia 2014년 10월 23일
편집: Julia 2014년 10월 23일

2 개 추천

Hi,
A =
1 2 3 4 5 6
23 45 10 90 11 34
>> A=A'
A =
1 23
2 45
3 10
4 90
5 11
6 34
>> B=sortrows(A,-2)
B =
4 90
2 45
6 34
1 23
5 11
3 10
>> B=B'
B =
4 2 6 1 5 3
90 45 34 23 11 10
Or short:
B=(sortrows(A',-2))'
B =
4 2 6 1 5 3
90 45 34 23 11 10

추가 답변 (3개)

Torsten
Torsten 2014년 10월 23일

1 개 추천

B=(sortrows(A',-2))';
where A is your input matrix.
Best wishes
Torsten.
Geoff Hayes
Geoff Hayes 2014년 10월 23일

0 개 추천

Ege - consider using sortrows to perform the above task
A = [1 2 3 4 5 6
23 45 10 90 11 34];
B = sortrows(A',-2)';
We transpose A so that we can sort on the second column. The negative indicates descending sort order. The result is then transposed to get the desired output as
B =
4 2 6 1 5 3
90 45 34 23 11 10
Guillaume
Guillaume 2014년 10월 23일
편집: Guillaume 2014년 10월 23일

0 개 추천

Transpose your matrix, use sortrows along the second column, transpose it back and flip it left to right to get descending order
m = [1 2 3 4 5 6
23 45 10 90 11 34];
fliplr(sortrows(m', 2)')

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

Ege
2014년 10월 23일

편집:

2014년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by