フィルターのクリア

Why are the "For loops" not looping?

4 ビュー (過去 30 日間)
Christopher Scott
Christopher Scott 2022 年 2 月 21 日
コメント済み: per isakson 2022 年 2 月 22 日
prompt = 'Enter Integer Value For N ';
N = input(prompt);
prompt = 'Enter Integer Value For E ';
E = input(prompt);
prompt = 'Enter Value For deltaE ';
deltaE = input(prompt);
[allState{N:-1:1}] = ndgrid(1:E); % Create N col matrix with all possible energy configurations
allState = reshape(cat(N+1,allState{:}),[],N);
allStateSum = sum(allState,2); % Sum a row of allState and place values into a col matrix
allStateSum = transpose(allStateSum);
deltaEmatrix = (0:deltaE:E+20); % create col matrix that increments by deltaE from 0 up to E
Omega = zeros(1,length(deltaEmatrix)); % Initialize Omega matrix, ready to accept values
p = 0; % The first "for loop" ascends from 1 to # of rows of deltaEmatrix
for pIndex = 1:length(deltaEmatrix)
p = p +1;
q = 0;
disp(pIndex)
for qIndex = 1:length(allStateSum) % Second "for loop" ascends from 1 to # of rows of allStateSum
q = q +1;
if allStateSum(1,q) >= deltaEmatrix(1,q) && allStateSum(1,q) < deltaEmatrix(1,q+1)
Omega(1,p) = Omega(1,p)+1;
else
return % This section loops through allStateSum and checks if value is between
end % the deltaE intervals. If true, add 1 to the pth row of Omega.
% Each value in allStateSum
% is checked
end
end
  1 件のコメント
per isakson
per isakson 2022 年 2 月 22 日
Replace line 23
return
by
continue
and look up help on the two.

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 2 月 22 日
I bet it's because of that return statement. When the code encounters a return the function is exited immediately (thus no more loop iterations will occur).

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by