How to do the following nested for loop?

1 次查看(过去 30 天)
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