Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB - create matrix with submatrices

Tags:

matrix

matlab

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!

like image 377
Kristian Avatar asked Sep 03 '26 21:09

Kristian


1 Answers

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.

like image 76
user1071136 Avatar answered Sep 05 '26 09:09

user1071136



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!