4
$\begingroup$

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!

$\endgroup$
  • $\begingroup$ BTW, 'Table[Join[c[[All, i]], cbar[[All, j]]], {j, 1, T}]' does not work. $\endgroup$ – Xavier Jul 6 at 12:52
6
$\begingroup$
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
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ @klgr PadRight[aa,n+p,bb] fails when n=4;p=3;m=2; for example? $\endgroup$ – creidhne Jul 7 at 10:00
  • $\begingroup$ Thank you @creidhne. Removed PadRight. $\endgroup$ – kglr Jul 7 at 11:49
4
$\begingroup$

appending the ith column of A and ith column of B

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

enter image description here

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

enter image description here

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

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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}]' $\endgroup$ – Xavier Jul 6 at 12:59
  • $\begingroup$ Thanks a lot, it works $\endgroup$ – Xavier Jul 6 at 13:02

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.