While Loop, question how to implement it

2 visualizaciones (últimos 30 días)
Raul Garcia
Raul Garcia el 14 de Oct. de 2019
Comentada: Rena Berman el 28 de Oct. de 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

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Oct. de 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 ?
  1 comentario
Raul Garcia
Raul Garcia el 14 de Oct. de 2019
Thanks a lot, after a while I had it done, but now with your answer I confirm my answer, thanks for your time!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Language Support en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by