How to create Matrix [1 2 3 4; 2 4 6 8; 3 6 9 12; 4 8 12 16] without using loops or any functions using loops?
26 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to generate matrix
A = [1 2 3 4; 2 4 6 8; 3 6 9 12; 4 8 12 16]
without using any loops, but I have trouble finding solution
0 个评论
采纳的回答
James Tursa
2022-3-9
编辑:James Tursa
2022-3-9
n = 4; % whatever
result = (1:n) .* (1:n)' % use implicit expansion row .* column
0 个评论
更多回答(2 个)
David Hill
2022-3-2
A = [1 2 3 4; 2 4 6 8; 3 6 9 12; 4 8 12 16];%what is wrong with this
You could also do something like:
n=4;
A=[1:n;2:2:2*n;3:3:3*n;4:4:4*n];
Davide Masiello
2022-3-2
编辑:Davide Masiello
2022-3-2
To make it more general
n = 4;
a = 1:n;
A = repmat(1:n,n,1).*a';
For instance, if n = 6 this will generate
A =
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
not sure if repmat uses loops though, it probably does.
5 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!