While Loop, question how to implement it
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Raul Garcia
am 14 Okt. 2019
Kommentiert: Rena Berman
am 28 Okt. 2019
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
1 Kommentar
Akzeptierte Antwort
Walter Roberson
am 14 Okt. 2019
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 ?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Language Support finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!