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

1 回表示 (過去 30 日間)
mrbond99
mrbond99 2020 年 7 月 6 日
コメント済み: mrbond99 2020 年 7 月 6 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by