For loop inside for loop

1 view (last 30 days)
Jihan Alfira
Jihan Alfira on 29 Nov 2020
Commented: Jihan Alfira on 29 Nov 2020
hello i need to create for loop inside for loop
i have 8 numbers, and i have to make an array 1x40 sized
so the hint is i have to make the first loop with 8 numbers, and the second loop to repeat 8 numbers into a 1x40 array
can you guys help me with the script?

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2020
The first group of values goes into columns 1 to 8. Call that group number 1, and associated indices 1 to 8.
The second group of values goes into columns 9 to 16. Call that group number 2, and associated indices 9 to 16.
Now what is the offset between the indices of the second group and the indices of the first group? well, (9:16)-(1:8) is all 8's.
The third group of values goes into columns 17 to 24. Call that group number 3, and associated indices 17 to 24.
Now what is the offset between the indices of the third group and the indices of the second group? Well, (17:24)-(9:16) is all 8's. What is the offset between the indices of the third group and the indices of the first group? Well, (17:24)-(1:8) is all 16's, which is 2*8 .
If you continue to the 4th group, the offset between the indices of the 4th group and the indices of the first group would be all 24's, which is 3*8 .
If you think about this for a moment, the offset between the indices for the N'th group and the 1st group is going to be (N-1)*8
So if you were to loop over group number, N, then the indices to write at would be at offset (N-1)*8 from the indices for plain loop that would be over 1:8
  3 Comments
Walter Roberson
Walter Roberson on 29 Nov 2020
for group_number = 1 : something
offset_to_start_of_group = a calculation involving group_number
for local_index = 1 : something
output(offset_to_start_of_group + local_index) = something
end
end
Jihan Alfira
Jihan Alfira on 29 Nov 2020
thank you so much! this help a lot

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!