2
votes

I have a square matrix of size C by C and I want to build a diagonal block matrix with it repeated N=2C(1+C) times. The problem is that the value of C can change, and so I don't know if I could use blkdiag, as I don't know the number of parameters I should enter because the size of the matrix is a variable that the user chooses. How could I do this in MATLAB?

1
Why can't you use blkdiag with variable parameters?Andras Deak
@AndrasDeak I don't know how to do it. Is it possible? I've always used blkdiag(A,A,A), for example. How could I write that without hardcoding the number of matrices?Tendero
Like this: construct a cell, then pass that to blkdiag (as a comma-separated list). Of course there might be a more straightforward approach; I admit that I didn't really think your issue through before asking:)Andras Deak
So I mean something like tmpcell = repmat({A},[1, 2*C*(1+C)]); out = blkdiag(tmpcell{:}).Andras Deak

1 Answers

5
votes

you can use

 M = kron(eye(N),A);

where A is the CxC matrix that repeats N times....

Because this will scale rather quickly , for sparse implementation use:

M = kron(speye(N),A);