필터 지우기
필터 지우기

how can i make a function which will return the largest and the second largest component?

조회 수: 2 (최근 30일)
how can i make a function that accepts as input a vector of integers, which will return the largest and the second largest component of the vector for the first i think that is max1=max(max(C)) any idea for max2?

채택된 답변

Jan
Jan 2012년 11월 18일
편집: Jan 2012년 11월 19일
Do you mean the maximum over a matrix (see tags and example max(max(C))) or over vector (body of the question)?
  1. Find maximum value with the corresponding index
  2. Set value of this index to NaN
  3. Find maximum again.
Alternative, which is slower due to the expensive sorting for large inputs:
  • SORT helps.
Which values should be replied, if the highest maximum is not unique?
[EDITED: Sorry, I've overseen that this is a homework!]
  댓글 수: 2
felix
felix 2012년 11월 18일
over vector. i have a vector with random number and i have to find the max and the second max number of the vector without using unique or short...
Jan
Jan 2012년 11월 19일
I think: "short" means "sort".
Sorry, felix, that I've solved your homework. Now it gets hard to submit it without cheating. Remember, that your teacher/assistent knwos this forum also, so it is strongly recommended to mention all sources.

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

추가 답변 (1개)

Jurgen
Jurgen 2012년 11월 18일
편집: Jurgen 2012년 11월 18일
Or:
if sum(C==max(C))==1
second = max(C(C<max(C)));
else
second = max(C);
end
i.e. test if the maximum is unique, if the max is not unique the second is equal to max(C)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by