Replace the values of a matrix by another values.
이전 댓글 표시
i have a 3*3 matrix like [750 360 500 ; 550 520 780 ; 150 350 270] I want to replace these values in the matrix by comparing each row from the largest to small. (i.e largest =1 ,second largest=2 and smallest =3 ). Finally is should get a matrix like [1 3 2 ;2 3 1 ;3 1 2]
채택된 답변
추가 답변 (1개)
Star Strider
2018년 7월 31일
This appears to do what you want:
A = [750 360 500 ; 550 520 780 ; 150 350 270];
[~,Idx] = sort(A,2,'descend'); % Sort Matrix Row-Wise, Descending, Return Indices
[~,Out] = sort(Idx,2) % Sort Indices Row-Wise, Ascending, Return Indices To Get Output
Out =
1 3 2
2 3 1
3 1 2
댓글 수: 2
jonas
2018년 7월 31일
+1
I figured there was a simpler solution...
Star Strider
2018년 7월 31일
Thank you.
This took a bit of experimenting to discover. It is probably robust, although I did not test it with other matrices.
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!