4
$\begingroup$

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?

$\endgroup$
8
$\begingroup$
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) $$

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you, this works very cleanly and easily. $\endgroup$ – Yogesh Pilania Jul 6 at 17:12
  • $\begingroup$ @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. $\endgroup$ – flinty Jul 7 at 1:37
7
$\begingroup$

How about

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}} ]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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}} $\endgroup$ – flinty Jul 6 at 17:03
  • $\begingroup$ Thanks @chris for the solution. $\endgroup$ – Yogesh Pilania Jul 6 at 17:13
5
$\begingroup$

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
| improve this answer | |
$\endgroup$
5
$\begingroup$

As suggested in this post (and a couple of others), there is the undocumented function SparseArray`SparseBlockMatrix 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 = SparseArray`SparseBlockMatrix[rules];

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

| improve this answer | |
$\endgroup$
4
$\begingroup$
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) $$

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.