How to get first 3 maximum number in a matrix

조회 수: 3 (최근 30일)
Moe
Moe 2014년 11월 4일
댓글: Adam 2014년 11월 5일
Suppose I have a matrix m:
m = [7;1;4;4;12;2;6;10;2];
I want to find first 3 maximum in matrix m, means like:
n = [7;12;10]; % sorting is not issue

채택된 답변

Adam
Adam 2014년 11월 4일
temp = sort( m, 'descend' );
n = temp(1:3);
  댓글 수: 2
Moe
Moe 2014년 11월 5일
Thanks. How to find index number of n?
Adam
Adam 2014년 11월 5일
[temp, idx] = sort( m, 'descend' );
n = temp(1:3);
idx = idx(1:3);

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 4일
n=sort(m)
n=n(end:end-2)

Matt J
Matt J 2014년 11월 5일
Bruno Luong took the trouble to make a fast MEX implementation

카테고리

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