필터 지우기
필터 지우기

Why in the code given below the number of iterations of the second - for loop ('j') decreases at each iteration of first - for loop ('l')?

조회 수: 2 (최근 30일)
for l=1:N-1
disp(['l: ',num2str(l)])
for j=1:N-l
disp(['j: ',num2str(j)])
length = length+1;
end
end
The output iterations are showing like this. Why 'j' is decreasing at each iteration of 'l'?
l: 1
j: 1
j: 2
j: 3
j: 4
j: 5
j: 6
j: 7
j: 8
j: 9
l: 2
j: 1
j: 2
j: 3
j: 4
j: 5
j: 6
j: 7
j: 8
l: 3
j: 1
j: 2
j: 3
j: 4
j: 5
j: 6
j: 7
l: 4
j: 1
j: 2
j: 3
j: 4
j: 5
j: 6
l: 5
j: 1
j: 2
j: 3
j: 4
j: 5
l: 6
j: 1
j: 2
j: 3
j: 4
l: 7
j: 1
j: 2
j: 3
l: 8
j: 1
j: 2
l: 9
j: 1

채택된 답변

VBBV
VBBV 2023년 6월 6일
For each iteration of I , the j counter is reduced by N- I instead of N-1
N = 5;
length = 0;
for I=1:N-1
disp(['l: ',num2str(l)])
% ------>> I
for j=1:N-I
disp(['j: ',num2str(j)])
length = length+1;
end
end
l: 1
j: 1 j: 2 j: 3 j: 4
l: 2
j: 1 j: 2 j: 3
l: 3
j: 1 j: 2
l: 4
j: 1
  댓글 수: 1
VBBV
VBBV 2023년 6월 6일
편집: VBBV 2023년 6월 6일
Every time J for loop is iterated, the limit for J loop iteration is changed by I for loop counter increment i.e when N = 3 and I increments from 1 to 2 , the max limit for J for loop is changed by 3-1 , 3-2 etc

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by