# Building a matrix from the columns of others

A is a n x m matrix, B is a matrix p x m, I wish to build C from appending the ith column of A and ith column of B, for all i from 1 to m so that C is a (n+p) x m matrix. How do I do that? All of n,m, p are parameters that may vary. Thanks!

• BTW, 'Table[Join[c[[All, i]], cbar[[All, j]]], {j, 1, T}]' does not work. Jul 6 '20 at 12:52

n = 4; p = 2; m = 3;

aa = Array[Subscript[a, ##] &, {n, m}];

bb = Array[Subscript[b, ##] &, {p, m}];


## Join

cc = Join[aa, bb];

TeXForm @ MatrixForm @ aa


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

TeXForm @ MatrixForm @ bb


$$\left( \begin{array}{ccc} b_{1,1} & b_{1,2} & b_{1,3} \\ b_{2,1} & b_{2,2} & b_{2,3} \\ \end{array} \right)$$

TeXForm @ MatrixForm @ cc


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

## ArrayReshape

cc2 = ArrayReshape[{aa, bb}, {n + p, m}]

cc2 == cc

 True


## Flatten

cc3 = Flatten[{aa, bb}, 1]

cc3 == cc

 True

• @klgr PadRight[aa,n+p,bb] fails when n=4;p=3;m=2;  for example? Jul 7 '20 at 10:00
• Thank you @creidhne. Removed PadRight.
– kglr
Jul 7 '20 at 11:49

appending the ith column of A and ith column of B

A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};


B = {{10, 11, 12}, {14, 15, 16}};


ArrayFlatten[{{A}, {B}}]


• Yes does work, however, I just saw that I had a mistake in my formula which I said did not work. It should read 'Table[Join[c[[All, j]], cbar[[All, j]]], {j, 1, T}]' Jul 6 '20 at 12:59
• Thanks a lot, it works Jul 6 '20 at 13:02