# How to assemble matrices?

I'm a beginner in Mathematica and have a very basic question.

Suppose I have made the following matrices (4x4). $$A,B,C,D,$$ and I want to make the larger matrix $$\begin{bmatrix} A & B \\ C & D \end{bmatrix}.$$

I wish to add to my question. I tried ArrayFlatten for the following: $$A$$ is a 4x2 matrix, $$B$$ is a 4x4 matrix, and $$C$$ is a 4x6 matrix. I tried this to construct a 4x12 matrix: X = ArrayFlatten[{A,B,C}] However, Dimensions[X] returns 3x4 in my notebook instead of 4x12. What I'm really trying to do is to join the matrices together. Please help. Thanks.

How to do this in Mathematica?

• ArrayFlatten[{{A, B}, {C, D}}] Commented May 30, 2019 at 19:36

Some example matrices matching your dimensions:

m1 = Array[Subscript[a,##]&, {4,2}];
m2 = Array[Subscript[b,##]&, {4,4}];
m3 = Array[Subscript[c,##]&, {4,6}];


For example:

m1 //TeXForm


$$\left( \begin{array}{cc} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \\ a_{3,1} & a_{3,2} \\ a_{4,1} & a_{4,2} \\ \end{array} \right)$$

The problem with your use of ArrayFlatten was that it was missing brackets. The following works fine:

ArrayFlatten[{{m1, m2, m3}}] //TeXForm


$$\left( \begin{array}{cccccccccccc} a_{1,1} & a_{1,2} & b_{1,1} & b_{1,2} & b_{1,3} & b_{1,4} & c_{1,1} & c_{1,2} & c_{1,3} & c_{1,4} & c_{1,5} & c_{1,6} \\ a_{2,1} & a_{2,2} & b_{2,1} & b_{2,2} & b_{2,3} & b_{2,4} & c_{2,1} & c_{2,2} & c_{2,3} & c_{2,4} & c_{2,5} & c_{2,6} \\ a_{3,1} & a_{3,2} & b_{3,1} & b_{3,2} & b_{3,3} & b_{3,4} & c_{3,1} & c_{3,2} & c_{3,3} & c_{3,4} & c_{3,5} & c_{3,6} \\ a_{4,1} & a_{4,2} & b_{4,1} & b_{4,2} & b_{4,3} & b_{4,4} & c_{4,1} & c_{4,2} & c_{4,3} & c_{4,4} & c_{4,5} & c_{4,6} \\ \end{array} \right)$$

Another alternative is to use Join:

Join[m1, m2, m3, 2] //TeXForm


$$\left( \begin{array}{cccccccccccc} a_{1,1} & a_{1,2} & b_{1,1} & b_{1,2} & b_{1,3} & b_{1,4} & c_{1,1} & c_{1,2} & c_{1,3} & c_{1,4} & c_{1,5} & c_{1,6} \\ a_{2,1} & a_{2,2} & b_{2,1} & b_{2,2} & b_{2,3} & b_{2,4} & c_{2,1} & c_{2,2} & c_{2,3} & c_{2,4} & c_{2,5} & c_{2,6} \\ a_{3,1} & a_{3,2} & b_{3,1} & b_{3,2} & b_{3,3} & b_{3,4} & c_{3,1} & c_{3,2} & c_{3,3} & c_{3,4} & c_{3,5} & c_{3,6} \\ a_{4,1} & a_{4,2} & b_{4,1} & b_{4,2} & b_{4,3} & b_{4,4} & c_{4,1} & c_{4,2} & c_{4,3} & c_{4,4} & c_{4,5} & c_{4,6} \\ \end{array} \right)$$

Finally, if you try to construct a matrix of matrices, another possibility is to use Flatten:

Flatten[{{m1, m2, m3}}, {{1,3}, {2,4}}] //TeXForm


$$\left( \begin{array}{cccccccccccc} a_{1,1} & a_{1,2} & b_{1,1} & b_{1,2} & b_{1,3} & b_{1,4} & c_{1,1} & c_{1,2} & c_{1,3} & c_{1,4} & c_{1,5} & c_{1,6} \\ a_{2,1} & a_{2,2} & b_{2,1} & b_{2,2} & b_{2,3} & b_{2,4} & c_{2,1} & c_{2,2} & c_{2,3} & c_{2,4} & c_{2,5} & c_{2,6} \\ a_{3,1} & a_{3,2} & b_{3,1} & b_{3,2} & b_{3,3} & b_{3,4} & c_{3,1} & c_{3,2} & c_{3,3} & c_{3,4} & c_{3,5} & c_{3,6} \\ a_{4,1} & a_{4,2} & b_{4,1} & b_{4,2} & b_{4,3} & b_{4,4} & c_{4,1} & c_{4,2} & c_{4,3} & c_{4,4} & c_{4,5} & c_{4,6} \\ \end{array} \right)$$

The nice thing about using Flatten is that the column dimensions don't have to match. For example:

Flatten[{{m1, m2}, {m3}}, {{1,3}, {2,4}}] //TeXForm


$$\left( \begin{array}{cccccc} a_{1,1} & a_{1,2} & b_{1,1} & b_{1,2} & b_{1,3} & b_{1,4} \\ a_{2,1} & a_{2,2} & b_{2,1} & b_{2,2} & b_{2,3} & b_{2,4} \\ a_{3,1} & a_{3,2} & b_{3,1} & b_{3,2} & b_{3,3} & b_{3,4} \\ a_{4,1} & a_{4,2} & b_{4,1} & b_{4,2} & b_{4,3} & b_{4,4} \\ c_{1,1} & c_{1,2} & c_{1,3} & c_{1,4} & c_{1,5} & c_{1,6} \\ c_{2,1} & c_{2,2} & c_{2,3} & c_{2,4} & c_{2,5} & c_{2,6} \\ c_{3,1} & c_{3,2} & c_{3,3} & c_{3,4} & c_{3,5} & c_{3,6} \\ c_{4,1} & c_{4,2} & c_{4,3} & c_{4,4} & c_{4,5} & c_{4,6} \\ \end{array} \right)$$

• what is the meaning of the second argument {{1,3},{2,4}}?
– D.B.
Commented May 30, 2019 at 21:06
• @D.B. See the answers to mathematica.stackexchange.com/q/119/45431. Commented May 30, 2019 at 21:10

Use ArrayFlatten. For example:

    With[{AA = Array[a, {4, 4}], BB = Array[b, {4, 4}],
CC = Array[c, {4, 4}], DD = Array[d, {4, 4}]},
ArrayFlatten[{{AA, BB}, {CC, DD}}]] // MatrixForm


$$\left( \begin{array}{cccccccc} a(1,1) & a(1,2) & a(1,3) & a(1,4) & b(1,1) & b(1,2) & b(1,3) & b(1,4) \\ a(2,1) & a(2,2) & a(2,3) & a(2,4) & b(2,1) & b(2,2) & b(2,3) & b(2,4) \\ a(3,1) & a(3,2) & a(3,3) & a(3,4) & b(3,1) & b(3,2) & b(3,3) & b(3,4) \\ a(4,1) & a(4,2) & a(4,3) & a(4,4) & b(4,1) & b(4,2) & b(4,3) & b(4,4) \\ c(1,1) & c(1,2) & c(1,3) & c(1,4) & d(1,1) & d(1,2) & d(1,3) & d(1,4) \\ c(2,1) & c(2,2) & c(2,3) & c(2,4) & d(2,1) & d(2,2) & d(2,3) & d(2,4) \\ c(3,1) & c(3,2) & c(3,3) & c(3,4) & d(3,1) & d(3,2) & d(3,3) & d(3,4) \\ c(4,1) & c(4,2) & c(4,3) & c(4,4) & d(4,1) & d(4,2) & d(4,3) & d(4,4) \\ \end{array} \right)$$

• Here is the result of your code $$\left( \begin{array}{ccccc} a(1,1) & a(1,2) & a(1,1) & a(1,2) & a(1,3) \\ c(1,1) & c(1,2) & d(1,1) & d(1,2) & d(1,3) \\ c(2,1) & c(2,2) & d(2,1) & d(2,2) & d(2,3) \\ \end{array} \right)$$ Is this it? Commented May 30, 2019 at 19:46
• My original answer used unequally sized rectangular matrices. I think such examples easily are made more specific without, but show (for example) how the sizes of the matrices must agree. Commented May 30, 2019 at 20:02