How can I create a matrix using nested loop functions?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to create a matrix of X and Y coordinates of nodes which are equally separated on a plane. The function should give coordinates for each node and write them all as a single matrix. It should start from zero, then it should go up along the Y-coordinate with a step of 'h' and it should write the coordinates into a corresponding row of a matrix with 2 columns (X and Y). When it reaches some value 'H' it should move by 'w' along the X-axis and start writing the coordinates of Y again (starting from zero).
Here is the code I wrote:
for i=1:Nw+1;
wi=0:w:W;
X=wi;
for n=1:Nh+1
hi=0:h:H;
Y=hi;
NC=[X(i) Y(n)]
end
end
This function works fine except for the fact that for each cycle it produces a separate 1x2 matrix, so for Nw=Nh=2 i get the results are presented by 9 separate matrices:
NC = 0 0
NC = 0 h
NC = 0 H
NC = w 0
NC = w h
NC = w H
NC = W 0
NC = W h
NC = W H
The values are exactly the ones I want, but I need them to be written in a single matrix of 2 columns and 9 rows (for this example). Nh, Nw and W, H are user's inputs. Also h=H/Nh and w=W/Nw.
I tried specifying the number of rows which is given by (Nh+1)*(Nw+1) and then write the values into each row, but I did not succeed.
So can anyone give me an idea of how I can do this?
Thanks.
0 个评论
采纳的回答
Mahdi
2013-4-17
With making the least amount of changes to your code, add this line before the for loop
NC=[];
And change the NC=[X(i) Y(n)] to
NC=[NC; X(i) Y(n)];
In the end, type NC to see the resultant matrix.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!