Find indicies of k smallest matrix elements

조회 수: 8 (최근 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 CenterFile Exchange에서 Crystals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by