Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Create a block diagonal matrix with same repeating block

I have a matrix K of dimensions n x n. I want to create a new block diagonal matrix M of dimensions N x N, such that it contains d blocks of matrix K as its diagonal.

I would have directly used M = blkdiag(K,K,K) etc. had d been smaller. Unfortunately, d is very large and I don't want to manually write the formula with d exactly same arguments for the blkdiag() function.

Is there any shorter, smarter way to do this?

like image 970
steadyfish Avatar asked May 04 '13 03:05

steadyfish


People also ask

How do you create a matrix diagonal matrix in Matlab?

D = diag( v ) returns a square diagonal matrix with the elements of vector v on the main diagonal. D = diag( v , k ) places the elements of vector v on the k th diagonal. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.

Do block diagonal matrices commute?

No, every symmetric matrix and the diagonal matrix doesnot commute. Hence the matrices will not commute.

Which command is used to extract diagonal elements of a matrix?

Extract Diagonal of Matrix Use the Extract Diagonal block to extract the diagonal of a matrix.


2 Answers

you can use kron for that.

M = kron(X,Y)

returns the Kronecker tensor product of X and Y. The result is a large array formed by taking all possible products between the elements of X and those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is m*p-by-n*q. So in your case something like this will do:

M = kron(eye(L),K)

with L the # of blocks.

like image 71
bla Avatar answered Sep 23 '22 03:09

bla


tmp = repmat({K},d,1);
M = blkdiag(tmp{:});

You should never use eval, or go into for loops unnecessarily. Kron is a very elegant way. Just wanted to share this as it also works.

like image 30
Kzk Avatar answered Sep 22 '22 03:09

Kzk