How to do the following nested for loop?
Show older comments
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
Accepted Answer
More Answers (1)
Davide Masiello
on 23 Jul 2023
Moved: Star Strider
on 23 Jul 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)
After you produced P1, at each iteration of your code you can just call the next row of the array P1
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!