Adding matrices (to assemble global stiffness matrix)

6 views (last 30 days)
Ryan Yeo
Ryan Yeo on 22 Nov 2021
Commented: Ryan Yeo on 23 Nov 2021
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 =
1 1 0
1 2 1
0 1 1
  1 Comment
David Hill
David Hill on 22 Nov 2021
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?

Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 22 Nov 2021
Edited: David Goodmanson on 23 Nov 2021
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;
Ryan Yeo
Ryan Yeo on 23 Nov 2021
Hi David,
Thank you for spotting the mistake! And thanks for clearing the confusion up, I've managed to obtain my global stiffness matrix!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by