Hi!
So I'm trying to create a matrix using a nested "for loop"
An example of a matrix that looks like:
could be coded using;
M = zeros(5,5);
for i = 1:5;
for j = 1:5
M(i,j) = j;
end
end
display(M)
For my matrix - i would like to create the following matrix using the same method i.e. nested loop:
but i am not getting very far...any ideas?

回答 (2 件)

Star Strider
Star Strider 2019 年 1 月 9 日

0 投票

I would use the ndgrid (link) function instead:
[X,Y] = ndgrid(3:3:9)
producing:
X =
3 3 3
6 6 6
9 9 9
Y =
3 6 9
3 6 9
3 6 9
Dinara Jafarova
Dinara Jafarova 2019 年 12 月 15 日

0 投票

M = zeros(3);
for i = 1:3
for j = 1:3
M(i,j)=3*i; %it can also be 3*j,as the matrix is 3x3
end
end
display(M)

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

リリース

R2014b

質問済み:

2019 年 1 月 9 日

回答済み:

2019 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by