필터 지우기
필터 지우기

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 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