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

1 次查看(过去 30 天)
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..

采纳的回答

Geoff Hayes
Geoff Hayes 2020-7-6
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 个评论
mrbond99
mrbond99 2020-7-6
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by