# Adding matrices (to assemble global stiffness matrix)

조회 수: 7(최근 30일)
Ryan Yeo 2021년 11월 22일
댓글: Ryan Yeo 2021년 11월 23일
I want to add several 4X4 matrices into one large one, see code below on method. This is to assemble a global stiffness matrix of several bar elements. I will have 3 matrices to add into 1 large one, but I am stuck in modifying the code to add 3 4x4 matrices into one 6x6.
A simple example:
C = {[1,1;1,1],[1,1;1,1]}; % <- all matrices
N = numel(C);
M = zeros(1+N,1+N);
for k = 1:N, M(k:k+1,k:k+1) = M(k:k+1,k:k+1)+C{k}; end
M
M =
1 1 0
1 2 1
0 1 1
##### 댓글 수: 1표시숨기기 없음
David Hill 2021년 11월 22일
You will have to explain more fully what you want to do. Adding 3 4x4 matrices (total elements=48) into 6x6 matrix (total elements= 36), how are you suppose to do that? Where is the overlap between matrices?

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

### 채택된 답변

David Goodmanson 2021년 11월 22일
편집: David Goodmanson 2021년 11월 23일
Hi Ryan,
here is an example. In the code below, a and b are the indices of the 4x4 submatrix of K that each stiffness submatrix is going to be dropped into. Here it's assumed that the upper left of each 4x4 submatrix is going to be put on the the diagonal of K, i.e. (1,1), (2,2) and (3,3) for K1,K2,K3 respectively, but even that does not have to be true. Not likely in this case, but you could have for example a1 = [1:4]; b1 = [2 :5]; which puts the upper left corner of K1 onto K(1,2).
a2 and b2 show that K2 can be spread out through K if that's what it takes.
K1 = randi(10,4,4);
K1 = K1 + K1'
K2 = randi(10,4,4);
K2 = K2 + K2'
K3 = randi(10,4,4);
K3 = K3 + K3'
K = zeros(6,6);
a1 = [1:4];
b1 = [1:4];
a2 = [2 3 5 6];
b2 = [2 3 5 6];
a3 = [3:6];
b3 = [3:6];
K(a1,b1) = K(a1,b1) + K1;
K(a2,b2) = K(a2,b2) + K2;
K(a3,b3) = K(a3,b3) + K3;
K
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Ryan Yeo 2021년 11월 23일
Hi David,
Thank you for spotting the mistake! And thanks for clearing the confusion up, I've managed to obtain my global stiffness matrix!

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

### Community Treasure Hunt

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

Start Hunting!

Translated by