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

1 vue (au cours des 30 derniers jours)
mrbond99
mrbond99 le 6 Juil 2020
Commenté : mrbond99 le 6 Juil 2020
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..

Réponse acceptée

Geoff Hayes
Geoff Hayes le 6 Juil 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 commentaire
mrbond99
mrbond99 le 6 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by