31
$\begingroup$

This is pretty trivial to do in Matlab, but I have not seen a function for it in Mathematica. Here is how you can do it with Join and Transpose. Is there an easier way to do it than this?

(*Concatenate Matrix Right*)
A1 = Table[Subscript[a1, i, j], {i, n}, {j, n}];
A2 = Table[Subscript[a2, i, j], {i, n}, {j, m}];
Transpose[Join[Transpose[A1], Transpose[A2]]] // MatrixForm

(*Concatenate  Matrix Below*)
A1 = Table[Subscript[a1, i, j], {i, n}, {j, n}];
A2 = Table[Subscript[a2, i, j], {i, m}, {j, n}];
Join[A1, A2] // MatrixForm

(*2X2 Block Matrix*)
A11 = Table[Subscript[a11, i, j], {i, n}, {j, n}];
A12 = Table[Subscript[a12, i, j], {i, n}, {j, m}];
A21 = Table[Subscript[a21, i, j], {i, m}, {j, n}];
A22 = Table[Subscript[a22, i, j], {i, m}, {j, m}];
Transpose[
Join[Transpose[Join[A11, A21]],Transpose[Join[A12, A22]]]] // MatrixForm

Sample output for n=3 and m=2:

enter image description here

$\endgroup$
4
  • 13
    $\begingroup$ Check out Join[mat1,mat2,2] for catting on the right. And ArrayFlatten might handle more complicated cases. $\endgroup$ Commented Nov 1, 2015 at 17:44
  • $\begingroup$ That was exactly what I was looking for! $\endgroup$
    – luke wendt
    Commented Nov 1, 2015 at 18:17
  • 1
    $\begingroup$ This is a related discussion Partitioned matrix operations. (And it is not something that can be done trivially in Matlab.) $\endgroup$ Commented Nov 2, 2015 at 1:12
  • $\begingroup$ Do you see those manually drawn lines in the matrixes in the question above? I made the MatrixFormDivided resource function so that you don't have to manually draw them. $\endgroup$ Commented Mar 14, 2023 at 17:39

3 Answers 3

36
$\begingroup$

As Daniel Lichtblau suggested in the comment, ArrayFlatten is the way.

  • ArrayFlatten[{{A1, A2}}] // MatrixForm gives your Out[198]
  • ArrayFlatten[{{A1}, {A2}}] // MatrixForm gives your Out[201]
  • ArrayFlatten[{{A11, A12}, {A21, A22}}] // MatrixForm gives your Out[206]
$\endgroup$
17
$\begingroup$

From the application tab on Join:

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ It often pays to read the docs. ;) $\endgroup$ Commented Nov 1, 2015 at 20:22
12
$\begingroup$

Just for fun, the more complex construction of block matrices may also be accomplished using SparseArray:

SparseArray[{
   Band[{1, 1}] -> A11, Band[{1, 4}] -> A12,
   Band[{4, 1}] -> A21, Band[{4, 4}] -> A22},
  {5, 5}
] // MatrixForm

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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