필터 지우기
필터 지우기

How do I gain indices of max values in 2D matrix?

조회 수: 8 (최근 30일)
Klara
Klara 2014년 4월 8일
답변: Jos (10584) 2014년 4월 10일
For a 16 x 12 matrix I should sort it in descending order to gain the max values (like 10 in total). After that, I should find what was the indices of those max values before sorting .
I tried several times with sort and find. But it doesn't give me correct result.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 8일
편집: Azzi Abdelmalek 2014년 4월 8일
You have two columns, you have to precise how you want to sort them
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 8일
You have a 16x12 matrix, there are several ways to sort your matrix, by rows or by columns? you have to give more details, or just show the result for a short example, if
A=[1 2;3 4;0 1;4 5 ;100 20;2 2;0 7]
What should be the result?

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

답변 (3개)

Walter Roberson
Walter Roberson 2014년 4월 8일
[sorted, idx] = sort(YourMatrix, 'rows', 'descend');
tenmax = idx(1:10);
  댓글 수: 2
Klara
Klara 2014년 4월 8일
Thanks but it gives me the indices of the max values, AFTER sorting!
Walter Roberson
Walter Roberson 2014년 4월 8일
[sorted, idx] = sortrows(YourMatrix, -(1:size(YourMatrix,2)));
tenmax = idx(1:10);
this will give you the indices of the original rows that got sorted to the corresponding row position.

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


Gautam
Gautam 2014년 4월 10일
The answer above gives you the indices of before sorting. Try this example.
A = [1 4; 6 2; 4 2; 7 3]
[sortedMat,sortedIndex] = sort(A,'descend')
The sortedIndex matrix gives indices of the original rows (Note here that each column is sorted independently)

Jos (10584)
Jos (10584) 2014년 4월 10일
Why sort first?
A = [1 4; 6 2; 4 2; 7 3]
[maxValues, RowIndex] = max(A,[],1)
LinearIndex = sub2ind(size(A),RowIndex, 1:size(A,2))
sortedA = sort(A,1,'descend')

카테고리

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