Nested loop code to fill a 10x100 matrix
显示 更早的评论
Hi all,
I'm pretty stuck on this one; I want to fill a 10x100 matrix as: A =
[1 2 3 . . .100
101 102 103 .
201 202 203 .
. . .
. . .
901 . . . . 1000]
Since I want to make use of for loops, I've made the following code:
u=10;
v=100;
A=zeros(u,v);
for m=1:u;
for n=1:v;
A(m,n)= ....
end
end
I manage to get the first row going from 1 to 100, but I can't seem to get the second row (and so on) to start with A(1,1)+m*100. I can guess that the problem is in the first loop, but I just can't seem to solve it. Anyone here who can help me getting the right code for A(m,n)=....? Or am I doing it completely wrong in the first place?
Thank you!
1 个评论
James Tursa
2017-9-15
Do you have to use loops because this is a homework assignment?
采纳的回答
更多回答(2 个)
Andrei Bobrov
2017-9-15
编辑:Andrei Bobrov
2017-9-15
out = (1:100) + (0:100:900)'
for older version of MATLAB:
out = bsxfun(@plus,1:100,(0:100:900)')
1 个评论
Note: See this example https://www.mathworks.com/matlabcentral/answers/356103-euclidean-distance-ed-calculation-in-matlab#answer_281678: The function is 10% faster with bsxfun compared to auto-expansion in R2016b. The auto-expansion is nice, but I have the impression, that the implementation is much less efficient than bsxfun.
Image Analyst
2017-9-15
A(m,n)= (m-1)*100+n;
类别
在 帮助中心 和 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!