Why can't it proceed to the next iteration of for 'runs' loop?

1 visualización (últimos 30 días)
nruns = 10; nphoton = 100; m = nphoton;
for runs = 1:nruns
q(:,runs) = rand(nphoton,1);
while m > 0
for i = 1:nphoton
s(i,runs) = 2*pi*q(i,runs);
m = m - 1;
end
end
end
Why array of s only save the first iteration of runs == 1? I can't solve this problem, too much loops. Please help..

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 6 de Jul. de 2020
mrbond99 - please look at the condition for the while loop
while m > 0
You will only enter this loop so long as m is positive...but the body of this loop decrements this variable on each iteration of the for loop. Since it is never reset to nphoton, then this code will never be executed again. I think that you want to move the decrement to this variable outside of the for loop.
for runs = 1:nruns
q(:,runs) = rand(nphoton,1);
while m > 0
for i = 1:nphoton
s(i,runs) = 2*pi*q(i,runs);
end
m = m - 1;
end
end
Or can you remove the m altogether and reduce the code to simply
for runs = 1:nruns
q(:,runs) = rand(nphoton,1);
for i = 1:nphoton
s(i,runs) = 2*pi*q(i,runs);
end
end
? Is the m necessary?
  1 comentario
mrbond99
mrbond99 el 6 de Jul. de 2020
I need to use m as it refer to how many photons have travelled through a step size, s. I think I solved this problem after reading your comment at ' ...never reset to nphoton'. Thank you very much, Geoff Hayes for your comments and solutions. I appreciate it very much.

Iniciar sesión para comentar.

Más respuestas (0)

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!

Translated by