How to stop counting an element in a for loop when it reaches the limit?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Cladio Andrea
el 26 de Mzo. de 2015
Respondida: Chad Greene
el 26 de Mzo. de 2015
Hello everyone, i have a for loop that increases from 1 to 3600 and i want an element inside that loop , that will be either 1 or 0 , but when it reaches 18 lets say, i mean if 1 is repeated 18 times in the loop i want it to stop having values of 1 , so the rest will be 0 again. Can anyone help please?
0 comentarios
Respuesta aceptada
Chad Greene
el 26 de Mzo. de 2015
Is this what you mean?
SomeNumbers = zeros(3600,1);
for k = 1:3600
RandomNumber = rand(1)-.8;
% Make a note when RandomNumber>0:
if RandomNumber>0
SomeNumbers(k) = 1;
end
% Quit the loop when 18 RandomNumbers have been >0:
if sum(SomeNumbers)==18
break
end
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!