필터 지우기
필터 지우기

While Loop, question how to implement it

조회 수: 2 (최근 30일)
Raul Garcia
Raul Garcia 2019년 10월 14일
댓글: Rena Berman 2019년 10월 28일
Hi everybody, I was wondering how to change a while loop with an “if” condition to an a simple while. For example I got
n=100;
x=rand(1,n);
k=1;
while k<n
if x(k) > 0.95
break;
end
k=k+1;
end
disp([num2str(k), is the index of the first element in x that is greater than 0.95]);
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2019년 10월 14일
n=100;
x=rand(1,n);
k=1;
while k<n && x(k) <= 0.95
k=k+1;
end
disp([num2str(k), is the index of the first element in x that is greater than 0.95]);
Why are you not permitting the k == n case? What do you want to do if none of the elements are greater than 0.95 ?
  댓글 수: 1
Raul Garcia
Raul Garcia 2019년 10월 14일
Thanks a lot, after a while I had it done, but now with your answer I confirm my answer, thanks for your time!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by