Skip to main content
3 of 4
added 1239 characters in body
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323

I think ArrayFlatten is your best friend here.

A = SparseArray[{{i_, i_} -> 1, {1, 2} -> a}, 2];
B = SparseArray[{{i_, i_} -> 1, {1, 2} -> b}, 2];

AB = ArrayFlatten[{{A,0},{0,B}}]

$$ \left( \begin{array}{cccc} 1 & a & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & b \\ 0 & 0 & 0 & 1 \\ \end{array} \right) $$

In this case, ArrayFlatten is even clever enough to replace the two 0s by the according 0-block matrices. This is not always possible, e.g., when you want to stack A and B on top of each other and if you still want to get a $4 \times 4$ matrix. Then you have to give at least one 0-block explicitly:

ArrayFlatten[{{A, SparseArray[{}, {2, 2}]}, {B, 0}}]
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323