Kristian
Kristian

Reputation: 1239

MATLAB - create matrix with submatrices

Say I have three different 2x2 submatrices, and I want to create a big 6x6 matrix in Matlab in which the three submatrices appear on the diagonal, and all the other values are 0. How can I do this wihtout entering all the zeros explicitly? I just started learning MatLab and I really am quite a novice. If someone could help me with this I would be very grateful! Later I have to do this with more matrices, and entering all the zeros is becoming a pain!

Upvotes: 3

Views: 7002

Answers (3)

AlFagera
AlFagera

Reputation: 91

Also one could define a=zeros(6); then you can start type a(1:2,1:2)=a11, a(3:4,3:4)=a22,a(5:6,5:6)=a33, where a11, a22 and a33 are your 2×2 matrices. A little bit long, but it works. Like another option.

Upvotes: 0

hossein
hossein

Reputation: 1

you can use

A=zeros(6,6)

to create a 6*6 matrix with all array zero.

Upvotes: -1

user1071136
user1071136

Reputation: 15725

Suppose your matrices are named M1, M2, M3, write blkdiag(M1, M2, M2). If any of your matrices is sparse, so will be the result. Otherwise it will be dense.

Upvotes: 7

Related Questions