How can I sort an array with two columns?

조회 수: 4 (최근 30일)
PJ
PJ 2018년 10월 8일
편집: Stephen23 2018년 10월 8일
If I have an array of student ID in column1 and GPAs in column2
Info = [52211 3.55; 52922 1.79; 51939 3.33; 12140 0.81]
How can I sort the array from the highest GPA to the lowest GPA.

채택된 답변

Stephen23
Stephen23 2018년 10월 8일
편집: Stephen23 2018년 10월 8일
Very simply in one line using sortrows:
>> Info = [52211,3.55;52922,1.79;51939,3.33;12140,0.81]
Info =
52211 3.55
52922 1.79
51939 3.33
12140 0.81
>> Info = sortrows(Info,-2)
Info =
52211 3.55
51939 3.33
52922 1.79
12140 0.81

추가 답변 (2개)

KSSV
KSSV 2018년 10월 8일
Info = [52211 3.55; 52922 1.79; 51939 3.33; 12140 0.81] ;
[val,idx] = sort(Info(:,2),'descend') ;
iwant = Info(idx,:)

Kevin Chng
Kevin Chng 2018년 10월 8일
Hi,
Info = [52211 3.55; 52922 1.79; 51939 3.33; 12140 0.81];
[~,I] = sort(Info(:,2),'descend');
Info(I,:);
Done!

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by