MATLAB Answers

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

14 views (last 30 days)
iB on 6 Nov 2019
Answered: Bob Nbob on 7 Nov 2019
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

Show 1 older comment
Bob Nbob on 6 Nov 2019
Are you adapting this code from a different program? This is not Matlab syntax.
In order to save the value from a loop into an array you need to assign the index. Something like the following:
for i = 1:10{
for j = 1:5{
x(i,j) = i*j;
mat
}}
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 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 Nbob 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;

0 Comments

Sign in to comment.

Sign in to answer this question.