Clear Filters
Clear Filters

How to increment matrix row only inside a nested for loop?

1 view (last 30 days)
for i= 1:10{
for j= 1:5{
x = i*j;
mat
}}
matrix's row number index row wise should increment by 1
save this x value in mat matrix's row wise(like 1st row,2nd row,...,50th row).
Please help
  4 Comments
iB
iB on 7 Nov 2019
for i =1 and j = 1 value should saved at row number 1 ; for i = 1 j = 5 value will be saved at row 5 and like that for i = 2 j = 1(in next i value increment) will be saved at row 6 then for i=2 j=2 at row 7 of matrix 'mat' like that till 50 (i *j)
i is 1 and j is 1 to 5
1 * 1 at row 1; 1*2 at row 2; 1*3 at row 3; 1*4 at row 4; 1*5 at row 5
now i incremented to 2 and j is 1 to 5 again so
2*1 at row 6; 2*2 at 7; 2*3 at 8; 2*4 at 9; 2*5 at 10
like that till i = 10 and j is 1 to 5
10*1 at 46; 10*2 at 47; 10*3 at 48; 10*4 at 49; 10*5 at 50
iB
iB on 7 Nov 2019
for i =1 and j = 1 value should saved at row number 1 ; for i = 1 j = 5 value will be saved at row 5 and like that for i = 2 j = 1(in next i value increment) will be saved at row 6 then for i=2 j=2 at row 7 of matrix 'mat' like that till 50 (i *j)
i is 1 and j is 1 to 5
1 * 1 at row 1; 1*2 at row 2; 1*3 at row 3; 1*4 at row 4; 1*5 at row 5
now i incremented to 2 and j is 1 to 5 again so
2*1 at row 6; 2*2 at 7; 2*3 at 8; 2*4 at 9; 2*5 at 10
like that till i = 10 and j is 1 to 5
10*1 at 46; 10*2 at 47; 10*3 at 48; 10*4 at 49; 10*5 at 50

Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 7 Nov 2019
The solution is indexing. You can do math in your index if you need to.
x((i-1)*5+j) = i*j;

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!