How to do the following nested for loop?

1 vue (au cours des 30 derniers jours)
M
M le 23 Juil 2023
Commenté : Voss le 15 Août 2023
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

Réponse acceptée

Voss
Voss le 23 Juil 2023
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 commentaire
Voss
Voss le 15 Août 2023
@M: Did this answer work for you?

Connectez-vous pour commenter.

Plus de réponses (1)

Davide Masiello
Davide Masiello le 23 Juil 2023
Déplacé(e) : Star Strider le 23 Juil 2023
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by