Find indicies of k smallest matrix elements

13 ビュー (過去 30 日間)
Brady Dean
Brady Dean 2020 年 9 月 29 日
コメント済み: madhan ravi 2020 年 9 月 29 日
I'm trying to find the indicies of smallest k matrix elements.
For example if I have
A = [10 5 2; 34 5 21; 4 6 8];
and I want to find the smallest 2 elements indicies then I want indicies 7 and 3.

採用された回答

Xingwang Yong
Xingwang Yong 2020 年 9 月 29 日
[~, indices] = sort(A(:), 'ascend');
ind = indices(1:k);
  1 件のコメント
Brady Dean
Brady Dean 2020 年 9 月 29 日
Ah, I forgot about the A(:) trick. This works perfectly.

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

その他の回答 (1 件)

Sindar
Sindar 2020 年 9 月 29 日
[~,idx] = mink(A,2);
  3 件のコメント
Brady Dean
Brady Dean 2020 年 9 月 29 日
I'm using R2020b, that's the newest isn't it?
mink gives minimums over a dimension, not the whole matrix. Sindar's code doesn't work for my example above.
madhan ravi
madhan ravi 2020 年 9 月 29 日
Unfortunately not everyone has the Crystal Ball.

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

カテゴリ

Help Center および File ExchangeCrystals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by