Why are the "For loops" not looping?

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

采纳的回答

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 CenterFile Exchange 中查找有关 Loops 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