필터 지우기
필터 지우기

How to do the following nested for loop?

조회 수: 3 (최근 30일)
M
M 2023년 7월 23일
댓글: Voss 2023년 8월 15일
How to do the following nested for loop?
if there is a vector P = [1 1 1 1 1 1]
and I want to change P in each loop as the following:
first outer loop
inner loop:
first it:
P = [0.1 1 1 1 1 1]
2nd it:
P = [0.2 1 1 1 1 1]
3rd
P = [0.3 1 1 1 1 1]
and so on until reach 0.9
2nd outer loop
P = [1 0.1 1 1 1 1]
2nd it:
P = [1 0.2 1 1 1 1]
3rd
P = [1 0.3 1 1 1 1]
and so on until reach 0.9
3nd outer loop
change the 3rd index as the above.. until reach the 6th outer loop

채택된 답변

Voss
Voss 2023년 7월 23일
format shortg
P = [1 1 1 1 1 1];
P_orig = P;
for ii = 1:numel(P)
fprintf('outer loop iteration %d\n',ii);
P = P_orig;
for jj = 1:9
fprintf('inner loop iteration %d\n',jj);
P(ii) = jj/10;
disp(P)
end
end
outer loop iteration 1
inner loop iteration 1
0.1 1 1 1 1 1
inner loop iteration 2
0.2 1 1 1 1 1
inner loop iteration 3
0.3 1 1 1 1 1
inner loop iteration 4
0.4 1 1 1 1 1
inner loop iteration 5
0.5 1 1 1 1 1
inner loop iteration 6
0.6 1 1 1 1 1
inner loop iteration 7
0.7 1 1 1 1 1
inner loop iteration 8
0.8 1 1 1 1 1
inner loop iteration 9
0.9 1 1 1 1 1
outer loop iteration 2
inner loop iteration 1
1 0.1 1 1 1 1
inner loop iteration 2
1 0.2 1 1 1 1
inner loop iteration 3
1 0.3 1 1 1 1
inner loop iteration 4
1 0.4 1 1 1 1
inner loop iteration 5
1 0.5 1 1 1 1
inner loop iteration 6
1 0.6 1 1 1 1
inner loop iteration 7
1 0.7 1 1 1 1
inner loop iteration 8
1 0.8 1 1 1 1
inner loop iteration 9
1 0.9 1 1 1 1
outer loop iteration 3
inner loop iteration 1
1 1 0.1 1 1 1
inner loop iteration 2
1 1 0.2 1 1 1
inner loop iteration 3
1 1 0.3 1 1 1
inner loop iteration 4
1 1 0.4 1 1 1
inner loop iteration 5
1 1 0.5 1 1 1
inner loop iteration 6
1 1 0.6 1 1 1
inner loop iteration 7
1 1 0.7 1 1 1
inner loop iteration 8
1 1 0.8 1 1 1
inner loop iteration 9
1 1 0.9 1 1 1
outer loop iteration 4
inner loop iteration 1
1 1 1 0.1 1 1
inner loop iteration 2
1 1 1 0.2 1 1
inner loop iteration 3
1 1 1 0.3 1 1
inner loop iteration 4
1 1 1 0.4 1 1
inner loop iteration 5
1 1 1 0.5 1 1
inner loop iteration 6
1 1 1 0.6 1 1
inner loop iteration 7
1 1 1 0.7 1 1
inner loop iteration 8
1 1 1 0.8 1 1
inner loop iteration 9
1 1 1 0.9 1 1
outer loop iteration 5
inner loop iteration 1
1 1 1 1 0.1 1
inner loop iteration 2
1 1 1 1 0.2 1
inner loop iteration 3
1 1 1 1 0.3 1
inner loop iteration 4
1 1 1 1 0.4 1
inner loop iteration 5
1 1 1 1 0.5 1
inner loop iteration 6
1 1 1 1 0.6 1
inner loop iteration 7
1 1 1 1 0.7 1
inner loop iteration 8
1 1 1 1 0.8 1
inner loop iteration 9
1 1 1 1 0.9 1
outer loop iteration 6
inner loop iteration 1
1 1 1 1 1 0.1
inner loop iteration 2
1 1 1 1 1 0.2
inner loop iteration 3
1 1 1 1 1 0.3
inner loop iteration 4
1 1 1 1 1 0.4
inner loop iteration 5
1 1 1 1 1 0.5
inner loop iteration 6
1 1 1 1 1 0.6
inner loop iteration 7
1 1 1 1 1 0.7
inner loop iteration 8
1 1 1 1 1 0.8
inner loop iteration 9
1 1 1 1 1 0.9

추가 답변 (1개)

Davide Masiello
Davide Masiello 2023년 7월 23일
이동: Star Strider 2023년 7월 23일
Must you do this with a nested loop?
Example
P0 = [(0.1:0.1:0.9)',ones(9,5)];
P1 = P0;
for i = 1:5
P1 = [P1;circshift(P0,i,2)];
end
disp(P1)
0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.1000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 1.0000 1.0000 1.0000 1.0000 1.0000 0.3000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.6000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 0.9000
After you produced P1, at each iteration of your code you can just call the next row of the array P1

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by