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

1 次查看(过去 30 天)
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 个评论
iB
iB 2019-11-7
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 2019-11-7
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

请先登录,再进行评论。

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by