MATLAB Answers

Concatenate x amount of matrices

8 views (last 30 days)
I have a FEM problem that i want to solve with a matlab script so that it can be used for future questions. I´m trying to concatenate x number of known 4x4 matrices diagonaly to form a large 2(x+1),2(x+1) matrix. Examples would be one 4x4 matrice into a 4x4 matrice or three 4x4 matrice into a 8x8 matrix. in the end i want it to look like this but on a bigger scale:
A=[k1 -k1;-k1 k1], B=[k2,-k2; -k2 k2] => C=[k1 -k1 0 ; 0 -k1 + k2 k2; 0 -k2 k2]
I have tried to solve this with this code:
K = sym(zeros( 2*(x+1) , 2*(x+1) ) );
for k = 1:x
K(k:2*((k+1)),k:(2*(k+1))) = K( k:(2*(k+1)) , k:(2*(k+1)) )+C{x};
end
This is part of a script there the user defines the amount of matrices. I made similar loop so i belive its the indexing within the loop that is the problem but dont know how to solve it. Is it possible to solve this problem simply within this loop?
Thanks in advance

  2 Comments

hosein Javan
hosein Javan on 12 Aug 2020
if you could write a general form of your matrix in an image or latex equation, it would be more comprehensible. I can see no pattern of how A & B result in C. please explain more
Hampus Augustsson
Hampus Augustsson on 13 Aug 2020
I see. I will attach an image that will hopefully make it more clear. This is what the smaller k1, k2 & k3 looks like and i think i have assembled the K matrix correctly. Sorry of it is unclear, got a bit cramped in the end

Sign in to comment.

Accepted Answer

David Hill
David Hill on 13 Aug 2020
x=length(C);
K=zeros(2*(x+1));
for i=1:x
K=K+blkdiag(repmat(zeros(2),i-1,i-1),C{i},repmat(zeros(2),x-i,x-i));
end

  0 Comments

Sign in to comment.

More Answers (2)


hosein Javan
hosein Javan on 13 Aug 2020
Edited: hosein Javan on 13 Aug 2020
after a few (or maybe a lot!) thinking, I found the pattern. I used symbolic to specify each element by its name rather than value. actually it wasn't really concatenation since every 3 element the matrices overlap and we are taking their sums. it works for any number of "k" matrices which in here you called "x".
In the case of 3 matrices:
k{1} = sym('k1_',4); k{1}
k{2} = sym('k2_',4); k{2}
k{3} = sym('k3_',4); k{3}
x = length(k); % number of "k" matrices
n = 4 + (x-1)*2; % dimension of matrix "K" concatenated(not exactly!)
K = sym(zeros(n)); % initilize "K" by all zeros
for i = 1:x
j = 2*i-1;
K(j:j+3,j:j+3) = K(j:j+3,j:j+3) + k{i};
end
K
the result:
k{1} =
[ k1_1_1, k1_1_2, k1_1_3, k1_1_4]
[ k1_2_1, k1_2_2, k1_2_3, k1_2_4]
[ k1_3_1, k1_3_2, k1_3_3, k1_3_4]
[ k1_4_1, k1_4_2, k1_4_3, k1_4_4]
k{2} =
[ k2_1_1, k2_1_2, k2_1_3, k2_1_4]
[ k2_2_1, k2_2_2, k2_2_3, k2_2_4]
[ k2_3_1, k2_3_2, k2_3_3, k2_3_4]
[ k2_4_1, k2_4_2, k2_4_3, k2_4_4]
k{3} =
[ k3_1_1, k3_1_2, k3_1_3, k3_1_4]
[ k3_2_1, k3_2_2, k3_2_3, k3_2_4]
[ k3_3_1, k3_3_2, k3_3_3, k3_3_4]
[ k3_4_1, k3_4_2, k3_4_3, k3_4_4]
K =
[ k1_1_1, k1_1_2, k1_1_3, k1_1_4, 0, 0, 0, 0]
[ k1_2_1, k1_2_2, k1_2_3, k1_2_4, 0, 0, 0, 0]
[ k1_3_1, k1_3_2, k1_3_3 + k2_1_1, k1_3_4 + k2_1_2, k2_1_3, k2_1_4, 0, 0]
[ k1_4_1, k1_4_2, k1_4_3 + k2_2_1, k1_4_4 + k2_2_2, k2_2_3, k2_2_4, 0, 0]
[ 0, 0, k2_3_1, k2_3_2, k2_3_3 + k3_1_1, k2_3_4 + k3_1_2, k3_1_3, k3_1_4]
[ 0, 0, k2_4_1, k2_4_2, k2_4_3 + k3_2_1, k2_4_4 + k3_2_2, k3_2_3, k3_2_4]
[ 0, 0, 0, 0, k3_3_1, k3_3_2, k3_3_3, k3_3_4]
[ 0, 0, 0, 0, k3_4_1, k3_4_2, k3_4_3, k3_4_4]

  0 Comments

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!