Azzera filtri
Azzera filtri

Why are the "For loops" not looping?

1 visualizzazione (ultimi 30 giorni)
Christopher Scott
Christopher Scott il 21 Feb 2022
Commentato: per isakson il 22 Feb 2022
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

Risposta accettata

Voss
Voss il 22 Feb 2022
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).

Più risposte (0)

Categorie

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

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by