How do you add unique values to each entry in a matrix via a loop?

2 次查看(过去 30 天)
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!
  2 个评论
Bob Thompson
Bob Thompson 2019-2-5
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?
KieranSQ
KieranSQ 2019-2-6
编辑:KieranSQ 2019-2-6
Thank you for your comment. So what I am trying to do is take each vertex in the square and see what it is connected to. From there I want to store the vertex index in a matrix where I can, later, pull out the values to be able to create a global matrix from a smaller local matrix. So I need to consider each vertex (1,2,.....,npoint) in this case i=1,2,3,4 and then each connection for each vertex.
If then I want to generalise to say 60 points I'd rather not need to explicitly type each value in.
Once this is created I am then hoping to use these entries to calculate a global matrix for the finite element method.
I hope that clears my question up slightly?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by