MATLAB: Block diagonal matrix of identity times scalar.

identitymatrixscalar

Hi,
I currently have a vector a = [1 2]', I wish to create a block diagonal matrix. Each block is identity (3×3) times the corresponding scalar in the vector a.
i.e. with a = [1 2]' I want to produce b = [1 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 0 0 0; 0 0 0 2 0 0; 0 0 0 0 2 0; 0 0 0 0 0 2]. The catch is a can be a vector of N x 1, thus b is of size 3*N x 3*N
The answer also has to be for loop free. I've tried using blkdiag() and eye() but have dimension issues in my multiplication

Best Answer

here is my take.
a=[1,2];
c = round(a(1):1/(length(b(:,1))-1):a(2));
matrix = diag(c);