How can I create a matrix using nested loop functions?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Mahdi
il 17 Apr 2013
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!