필터 지우기
필터 지우기

How to get the first five minimum number for a list

조회 수: 5 (최근 30일)
amj
amj 2018년 2월 11일
댓글: amj 2018년 2월 11일
Hi,
I would like to get the index number of the first five minimum number from the list. This is my code:
list = [4,10,1,2,7,12,5,6,10,8];
min=list(1,1)
ind = 1;
N = length(list)
t= 0;
while (t<5)
for n = 2:N
if min>list(n)
ind=n;
min=list(n);
end
end
minIndexList(1,t+1) =ind ;
t = t+1;
end
disp(minIndexList)
However, the code just stored and repeat the index of the minimum Fors insance, minIndexList just stored >> 3,3,3,3,3. By right, the minIndexList should store = 3,4,1,7,8. Do anyone can help me to solve this problem? Thank you in advance
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 2월 11일
What behaviour do you want if the list has duplicates? Are you looking for the indices of the 5 smallest unique values, or the 5 smallest values including duplicates?
What output would you expect for the list [1 1 1 2 2 2 2 3 3 3 3 3 3] ? You "use up" 3 of the output slots for the 1's, so then you have 2 output slots remaining but 4 locations that have the same minimum value: how do you decide which of those 4 locations you give the index of?

댓글을 달려면 로그인하십시오.

채택된 답변

Birdman
Birdman 2018년 2월 11일
Use the simple code below:
[~,idx]=ismember(sort(list),list);
idx=idx(1:5)
  댓글 수: 3
Birdman
Birdman 2018년 2월 11일
Yes but still does the job.
amj
amj 2018년 2월 11일
Thanks a million Birdman & Walter Roberson

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 2월 11일
편집: Walter Roberson 2018년 2월 11일
  댓글 수: 1
amj
amj 2018년 2월 11일
Thank you Walter Roberson. Yeah ..indeed true. Any idea if the repeating indexes occur?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by