# How to form a diagonal matrix from sub-matrices?

I have a 3x3 matrix (let's say G) and a 3x3 matrix of zeros (let's say zero). I want a diagonal matrix in the form Diag[G; G; G; zero] such that the size of matrix becomes 12x12. I actually have to add this diagonal matrix to another 12x12 matrix.

I have already tried the solutions of How to form a block-diagonal matrix from a list of matrices? and Create a matrix of matrices using Band and ArrayFlatten but they don't give me a correct result when I check the dimensions of the diagonal matrix. Is there a method that I can use to achieve this result?

g = ArrayReshape[Range[9], {3, 3}];
m = KroneckerProduct[DiagonalMatrix[{1, 1, 1, 0}], g]
m // MatrixForm


$$\left( \begin{array}{cccccccccccc} 1 & 2 & 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 4 & 5 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 7 & 8 & 9 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 2 & 3 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 4 & 5 & 6 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 7 & 8 & 9 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 2 & 3 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 4 & 5 & 6 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 7 & 8 & 9 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$

• Thank you, this works very cleanly and easily. Jul 6, 2020 at 17:12
• @YogeshPilania the nice thing about the Kronecker product is you can place a sub-matrix everywhere a 1 appears in its argument matrix with all other entries zero, so it's easy to extend to other patterns. Jul 7, 2020 at 1:37

G = Array[a, {3, 3}];
Format[a[i_, j_]] = Subscript[a, i, j];
B = ArrayFlatten[{{G,0,0,0},{0,G,0,0}, {0,0,G,0},{0,0,0*G}} ]


• This is 9x9. OP wanted 12x12 - I think you need to change it to {{G,0,0,0},{0,G,0,0}, {0,0,G,0},{0,0,0,0}} Jul 6, 2020 at 17:03
• Thanks @chris for the solution. Jul 6, 2020 at 17:13

As suggested in this post (and a couple of others), there is the undocumented function SparseArraySparseBlockMatrix that allows you to do that quite efficiently.

g = ArrayReshape[Range[9], {3, 3}];
rules = {{1, 1} -> g, {2, 2} -> g, {3, 3} -> g, {4, 4} -> 0 g};
A = SparseArraySparseBlockMatrix[rules];


This function allows the argument pattern SparseArraySparseBlockMatrix[rules,dims,background], where dims stands probably for the total matrix dimension. But apparently, there is a bug and SparseArraySparseBlockMatrix just ignores any given second argument. So make sure that the first and last diagonal block are always present.

I am always fond of the outer product in situations like this...

g = Partition[Range[9], 3];
m = ArrayFlatten@Outer[Times, DiagonalMatrix[{1, 1, 1, 0}], g]
m // MatrixForm

G = {{a, b, c}, {d, e, f}, {g, h, i}};
s = Normal[SparseArray[Band[{1, 1}] -> {G, G, G}, {12, 12}]] // MatrixForm


$$\left( \begin{array}{cccccccccccc} a & b & c & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ d & e & f & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ g & h & i & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & a & b & c & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & d & e & f & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & g & h & i & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & a & b & c & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & d & e & f & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & g & h & i & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$$