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

1 visualizzazione (ultimi 30 giorni)
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..

Risposta accettata

Geoff Hayes
Geoff Hayes il 6 Lug 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 Commento
mrbond99
mrbond99 il 6 Lug 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by