For loop within for loop
4 次查看(过去 30 天)
显示 更早的评论
Please help me out. This code isn't working...
qq=8:10008;
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq;
end
end
I just want answer for A
3 个评论
Alex Mcaulley
2019-8-9
编辑:Alex Mcaulley
2019-8-9
You can do it without loop. For example:
qq = 8:10008;
n = 1:10001;
m = 1:9;
[X,Y] = meshgrid(qq + n,m)
A = X + Y;
If you use the loops, at least preallocate your array A to minimize the execution time:
qq = 8:10008;
A = zeros(9,10001);
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq(n);
end
end
回答(1 个)
Jos (10584)
2019-8-9
In recent ML versions there is no need for meshgrid or so. The plus syntax will expand the vectors :-)
% a smaller example
n = 1:11
m = 1:4
q = 8:18
A = m' + n + q
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!