How do you add unique values to each entry in a matrix via a loop?
1 view (last 30 days)
Show older comments
I am trying to create matrix in a loop such that we are recording each vertex per edge on this (or of any number of verticies) square - apologies for the poorly drawn square. From here I want to record each vertex with connected edge like:
M= 1 2 5 0 0 0 0
1 2 3 5 6 0 0
.............
(6) 2 3 5 6 7 9 10
......................
12 15 12 0 0 0
where 0 represent that there are no other connections (to give an nxn matrix). I am doing this to build a connection matrix for a finite element process, so I can build global matricies.
My initial thoughts are
%npoint in this case is 4.
nodes=zeros(npoint^2,npoint)
for i=1:npoint
for j=1:npoint
if i<=npoint && j<=npoint
nodes(i,j)=i
nodes(i,i+npoint-3)=i+1
nodes(i,i+npoint-2)=i+npoint
end
end
end
but this just overwrites my entries - as you would expect.
If someone could offer any adivse on how to do this or a better approach, I would appreciate it greatly!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/202895/image.jpeg)
2 Comments
Bob Thompson
on 5 Feb 2019
I'm a little bit confused what you're looking for here. The code you posted seems to only print values into the array for the final value of each loop. If you only want to print values for when i and j == npoint then why not just specify that that is the index, rather than looping through all of the other values?
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!