I want to construct a square block matrix out of square matrices, whose entries all lie on their respective diagonals. For example,
clearN = 3;M = 5;MFull = zeros(N * M, N * M); for n = 1 : M for m = 1 : M Mblock = rand(N,1) * m * n; MFull((n - 1) * N + 1:n * N, (m - 1) * N + 1:m * N) = diag(Mblock); endend
When this matrix becomes big, this is clearly inefficient. I would like to define MFull as a sparse matrix to avoid memory issues and speed things up. Any suggestions are appreciated! To clarify: I would like to avoid creating MFull as a full matrix, and then converting it to a sparse matrix.
Best Answer