Sort matrix to get the row and column position of sorted matrix

조회 수: 2 (최근 30일)
Elysi Cochin
Elysi Cochin 2020년 3월 21일
편집: Ameer Hamza 2020년 3월 21일
how should i sort the matrix
M = [0,15,14,13,10,7,4,1,2;
0,0,9,6,4,12,1,3,1;
0,0,0,10,8,5,3,1,2;
0,0,0,0,17,2,11,1,4;
0,0,0,0,0,1,12,1,7;
0,0,0,0,0,0,1,7,5;
0,0,0,0,0,0,0,3,9;
0,0,0,0,0,0,0,0,8];
to get the sorted index positions as
[4,5], [1,2], [1,3], [1,4], [2,6], [5,7], [4,7], [1,5].....

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 21일
편집: Ameer Hamza 2020년 3월 21일
[~,idx] = sort(M(:), 'descend');
idx(M(:)==0) = [];
[row, col] = ind2sub(size(M), idx);
indexes = [row col];
Result:
indexes =
4 5
1 2
1 3
1 4
2 6
5 7
4 7
3 4
1 5
.......

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