How to do the following nested for loop?

1 回表示 (過去 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 件のコメント
Voss
Voss 2023 年 8 月 15 日
@M: Did this answer work for you?

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by