필터 지우기
필터 지우기

sort a matrix based on a maximum value in each row

조회 수: 2 (최근 30일)
Amine Ben Ayara
Amine Ben Ayara 2016년 2월 5일
댓글: Star Strider 2016년 2월 5일
I have a matrix that I first determined the max value in each row and its row index. now I need to sort the whole matrix in descendant order based on the maximums identified from the row with the larget max to the row with smallest max. example: A=[0 1 2 1 1; 0 4 3 1 2; 1 2 0.5 3 5] ( matrix is 3*5. so first I need to identify the Max in each row and its index. so now it [2; 4; 5] then I need to reorder the matrix/ all the rows based on descendant order, so first row is the one with the highest max: SortedMatrix=[ 1 2 0.5 3 5;0 4 3 1 2;0 1 2 1 1]; any suggestions? please.

채택된 답변

Star Strider
Star Strider 2016년 2월 5일
This works:
A=[0 1 2 1 1; 0 4 3 1 2; 1 2 0.5 3 5];
Arowmax = max(A, [], 2);
[~,idx] = sort(Arowmax, 'descend');
SortedMatrix = A(idx,:)
SortedMatrix =
1 2 0.5 3 5
0 4 3 1 2
0 1 2 1 1
  댓글 수: 2
Amine Ben Ayara
Amine Ben Ayara 2016년 2월 5일
Star, thank you so much, I tried this and it seems like the perfect solution. Now Im going to duplicate the process with my (15000*152) matrix! Thank you for saving my day! Can I be your best friend forever! BFF!! haha
Star Strider
Star Strider 2016년 2월 5일
My pleasure!
Quite definitely!

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

추가 답변 (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