i would like to use the loop for that looks for the maximum of a vector and its position without using the 'max.can anyone help me ?

댓글 수: 3

diadalina
diadalina 2017년 11월 21일
편집: per isakson 2017년 11월 21일
i had tried this, but i doesn't give me what i want,
maxi=x(1);
for i=1:length(x)
%
if x(i)>maxi
maxi=x(i);
indice=i;
else
maxi=maxi;
indice=i;
end
end
KL
KL 2017년 11월 21일
what have you tried so far?
diadalina
diadalina 2017년 11월 21일
편집: diadalina 2017년 11월 21일
can you help me, please ?

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

 채택된 답변

KL
KL 2017년 11월 21일

0 개 추천

you have got it almost right, you just need to put them inside the loop. I'll give you some tips to work further.
  • first assign the first value of the vector to maxi and set indice to 1.
maxi = x(1);
indice = 1;
  • then loop through number of elements of x starting from 2 (since we have assigned first value of x to maxi),
for k=2:numel(x)
if(x(k)>maxi)
%and the remainig logic
end %end of if
end %end of for
  • in your if else condition, you do not have to assign maxi=maxi for the else case, you can simply write continue;. This will continue to the next iteration.

댓글 수: 1

diadalina
diadalina 2017년 11월 21일
thank you Mr,Kl, , i had got this,
maxi=x(1);indice=1;
for i=2:length(x)
if x(i)>maxi
maxi=x(i);
indice=i;
end
end
i think, that's the right way, thank you for your tips.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 11월 21일

댓글:

2017년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by