How to find index of first 3 maximum number in matrix

8 ビュー (過去 30 日間)
Moe
Moe 2014 年 11 月 5 日
回答済み: Aida Arman 2018 年 11 月 21 日
I have the following function that n is the first 3 maximum value of matrix m:
m = [7;1;4;4;12;2;6;10;2];
temp = sort( m, 'descend' );
n = temp(1:3)
I need to know the index of matrix n, like:
p = [5;8;1]; % first max value in matrix m is 12 that it located in the fifth row

採用された回答

Orion
Orion 2014 年 11 月 5 日
編集済み: Orion 2014 年 11 月 5 日
use the second output argument of sort
m = [7;1;4;4;12;2;6;10;2];
[temp,originalpos] = sort( m, 'descend' );
n = temp(1:3)
p=originalpos(1:3)
  1 件のコメント
Amit Kumar
Amit Kumar 2018 年 2 月 21 日
編集済み: Amit Kumar 2018 年 2 月 21 日
Great.. I just want to add a comment that if you have NaN in your matrix replace beforehand as m(isnan(m))=0 and then use, as:
m = [7;1;4;4;NaN;2;6;10;2];
m(isnan(m))=0
[temp,originalpos] = sort( m, 'descend' );
n = temp(1:3)
p=originalpos(1:3)

サインインしてコメントする。

その他の回答 (2 件)

Aida Arman
Aida Arman 2018 年 11 月 21 日

MA
MA 2014 年 11 月 5 日
編集済み: MA 2014 年 11 月 5 日
m = [7;1;4;4;12;2;6;10;2];
temp = sort( m, 'descend' );
n = temp(1:3);
for i=1:9
if n(1)==m(i)
p1=i;
elseif n(2)==m(i)
p2=i;
elseif n(3)==m(i)
p3=i;
end
end
p=[p1 p2 p3]

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by