필터 지우기
필터 지우기

Return Indices for x Smallest/Largest Values in Array

조회 수: 4 (최근 30일)
Nicholas Sbalbi
Nicholas Sbalbi 2020년 6월 4일
댓글: David Hill 2020년 6월 4일
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.

채택된 답변

Steven Lord
Steven Lord 2020년 6월 4일
Use the second output of the mink or maxk functions.

추가 답변 (2개)

David Hill
David Hill 2020년 6월 4일
[~,idx]=sort(x);
minIdx=idx(1:3);%for minimum 3
maxIdx=idx(end:-1:end-2);%for maximum 3

Sulaymon Eshkabilov
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by