Find indicies of k smallest matrix elements

8 Ansichten (letzte 30 Tage)
Brady Dean
Brady Dean am 29 Sep. 2020
Kommentiert: madhan ravi am 29 Sep. 2020
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.

Akzeptierte Antwort

Xingwang Yong
Xingwang Yong am 29 Sep. 2020
[~, indices] = sort(A(:), 'ascend');
ind = indices(1:k);
  1 Kommentar
Brady Dean
Brady Dean am 29 Sep. 2020
Ah, I forgot about the A(:) trick. This works perfectly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sindar
Sindar am 29 Sep. 2020
[~,idx] = mink(A,2);
  3 Kommentare
Brady Dean
Brady Dean am 29 Sep. 2020
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 am 29 Sep. 2020
Unfortunately not everyone has the Crystal Ball.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by