Return Indices for x Smallest/Largest Values in Array
2 ビュー (過去 30 日間)
古いコメントを表示
Is there a clean and elegant way for extracting the indices of the x smallest or largest values in an array? For example say I have an array [1,6,4,9,0] and I want the indices of the 3 smallest values, [1,3,5]. The functions min/max only return the index for the single minimum/maximum.
0 件のコメント
採用された回答
その他の回答 (2 件)
David Hill
2020 年 6 月 4 日
[~,idx]=sort(x);
minIdx=idx(1:3);%for minimum 3
maxIdx=idx(end:-1:end-2);%for maximum 3
0 件のコメント
Sulaymon Eshkabilov
2020 年 6 月 4 日
編集済み: Sulaymon Eshkabilov
2020 年 6 月 4 日
here is one of the possible ways:
A = [...; ...; ...];
[Row1,Col1]=find(max(A(:))==A) % Shows row# and col# of largest element
[Row2,Col2]=find(min(A(:))==A) % Shows row# and col# of smallest element
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!