Effacer les filtres
Effacer les filtres

Why are the "For loops" not looping?

4 vues (au cours des 30 derniers jours)
Christopher Scott
Christopher Scott le 21 Fév 2022
Commenté : per isakson le 22 Fév 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
  1 commentaire
per isakson
per isakson le 22 Fév 2022
Replace line 23
return
by
continue
and look up help on the two.

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 22 Fév 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).

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by