필터 지우기
필터 지우기

What is wrong with my for loop?

조회 수: 1 (최근 30일)
Kyle McLaughlin
Kyle McLaughlin 2020년 10월 28일
댓글: madhan ravi 2020년 10월 29일
%check for stitching global nxn matrix
elements = 4;
nodes = 5; %nodes
s=3; %size of matrix nxn
k=[1 1 1; 1 1 1; 1 1 1];
size = (s* elements) - (elements-1);
K = zeros(size,size);
K(1:s,1:s) = k(1:s,1:s);
%this fills the matrix with values
for i =2:elements-1
% K((s:((i*s)-(i-1))), (s:((i*s)-(i-1)))) = k(1:s,1:s);
for j = 1:elements-1
K((j*s-j-1):(i*s-i-1), (j*s-j-1):(i*s-i-1)) = k(1:s,1:s);
end
% K((s:((2*s)-(2-1))), (s:((2*s)-(2-1)))) = k(1:s,1:s);
% K((s:((3*s)-(3-1))), (s:((3*s)-(3-1)))) = k(1:s,1:s);
%K(s:((j*s)-(j-1)),s:((j*s)-(j-1))) = k(1:s,1:s);
% K(s:5,s:5) = k(1:s,1:s);
% K(5:7,5:7) = k(1:s,1:s);
% K(7:9,7:9) = k(1:s,1:s);
end
%this adds matrix elements at stitching location
for i = 1:elements-1
K(((i*s)-(i-1)), ((i*s)-(i-1))) = k(1,1)+k(s,s);
end
K
the lines including K(s:5,s:5) to K(7:9,7:9) was my verification to see if the matrix was right and it is. the code above in the for loops should give me the same answers as these but I keep getting an error saying unable to perform assignment because the size of the left matrix doesnt match the right
this is the error I recieve:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is
3-by-3.
  댓글 수: 4
Kyle McLaughlin
Kyle McLaughlin 2020년 10월 28일
I deleted it to post it correctly so there would be no further confusion
Kyle McLaughlin
Kyle McLaughlin 2020년 10월 29일
편집: Kyle McLaughlin 2020년 10월 29일
I was able to come up with the following code which is closer to what I want but doesnt quite fit:
elements = 4;
nodes = 5; %nodes
s=3; %size of matrix nxn
k=[1 1 1; 1 1 1; 1 1 1];
size = (s* elements) - (elements-1);
K = zeros(size,size);
%K(1:s,1:s) = k(1:s,1:s);
for j = 2:elements
for ii = 0: elements -2
%K(j*s-s+ii*(s-1):j*s-1+ii*(s-1), j*s-s+ii*(s-1):j*s-1+ii*(s-1)) = k(1:s,1:s);
%K(j*s-s-1:j*s-1, j*s-s-1:j*s-1) = k(1:s,1:s);
K(j*s-s-ii:j*s-1-ii,j*s-s-ii:j*s-1-ii)= k(1:s,1:s);
end
end
%K(size-s+1:size,size-s+1:size) = k(1:s,1:s);
for i = 1:elements-1
K(((i*s)-(i-1)), ((i*s)-(i-1))) = k(1,1)+k(s,s);
end
K
K =
1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0 0
1 1 2 1 1 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0 0
0 0 1 1 2 1 1 0 0 0 0
0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 1 1 2 1 1 0 0
0 0 0 0 0 1 1 1 1 1 0
0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1

댓글을 달려면 로그인하십시오.

채택된 답변

Kyle McLaughlin
Kyle McLaughlin 2020년 10월 29일
elements = 4;
s=3; %size of matrix nxn
k=[1 1 1; 1 1 1; 1 1 1];
length = (s* elements) - (elements-1);
K = zeros(length,length);
for j = 0:elements-1
index =(1:s);
n = index + j*(s-1);
K(n,n) = k(1:s,1:s);
end
for i = 1:elements-1
K(((i*s)-(i-1)), ((i*s)-(i-1))) = k(1,1)+k(s,s);
end
K
This does exactly what I want I answered my own question nevermind. Here it is for reference.
  댓글 수: 3
Kyle McLaughlin
Kyle McLaughlin 2020년 10월 29일
Excuse me, clearly i didnt see any answer you posted which is why it was deleted. Sorry you didnt answer my question and get an accepted answer. I am aware of how it works now, dont beat a dead horse.
madhan ravi
madhan ravi 2020년 10월 29일
Lol, good luck

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2020년 10월 28일
편집: Matt J 2020년 10월 28일
It seems clear that when i=j, the left hand side of
K((j*s-j-1):(i*s-i-1), (j*s-j-1):(i*s-i-1)) = k(1:s,1:s);
will be 1x1 whereas the right hand side will always be 3x3. Generally speaking, the size of the left hand side is changing in a highly i,j-dependent way whereas the right hand side is not.
  댓글 수: 3
Matt J
Matt J 2020년 10월 29일
I'm not sure what you are trying to achieve. If the idea is just to make tiled copies of k, then youcould just use repmat
K=repmat(k,elements,elements)
Kyle McLaughlin
Kyle McLaughlin 2020년 10월 29일
편집: Kyle McLaughlin 2020년 10월 29일
This is the begining of an FEA script, the code that I am writing here assembles the global stiffness matrix from the element matricies. I need to apply this to the mass matrix and damping matrix for nxn size so that it is modular in the event of different sized element matrixies for given element types. The goal is to add the first and lass matrix values which creates the nodal connection between the two elements and apply that to the lot of them. The first comment I made in the post, directly under my question, shows the desired results of the matrix.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by