# An intuitive way to form an augmented matrix

Given an $m\times n$ matrix $A$ and a $m\times 1$ vector $b$, what is the most "intuitive" way of forming the augmented matrix $[ A\ b]$? The most concise way that I know of is Flatten/@Transpose[{A,b}], but for beginners to Linear Algebra and/or Mathematica, this is hardly intuitive. In MATLAB, it is just [A b].

Is there a built-in command that does this simply that I've missed?

• Are you aware of this thread: Elegant operations on matrix rows and columns? Might be a duplicate at that... Sep 22, 2014 at 15:08
• I'm not necessarily asking "how to add a column", but rather, "How to add a column intuitively." I use Mathematica in my classroom and often row operations/Gaussian Elimination is taught before the transpose. So augmenting a matrix with a double transpose or with the Flatten command is confusing to students. MATLAB's method is much, much more intuitive. I want to know if there is a simple way of doing it. Given the links and other posts, it seems the answer is no. Sep 22, 2014 at 15:24
• If b is a vector (dimension=1) could do Join[A, List /@ b, 2]. If it is a 3x1 matrix then drop the List/@ part. Sep 22, 2014 at 16:21
• The original question would be a bit more clear if A and b were defined via Mathematica constructs. Especially what is b. Dec 10, 2023 at 23:39

I think the analogue of Matlab's implicit concatenation is ArrayFlatten, but it still needs to be called explicitly.

(aa = Table[a[i, j], {i, 3}, {j, 3}]) // MatrixForm

$$\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) \\ \end{array} \right)$$

(bb = Table[{b[i]}, {i, 3}]) // MatrixForm

$$\left( \begin{array}{c} b(1) \\ b(2) \\ b(3) \\ \end{array} \right)$$

ArrayFlatten[{{aa, bb}}] // MatrixForm

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

To work with the usual form of vectors, I'd suggest the following:

row[v_] := {v}
col[v_] := {#} & /@ v
sca[x_] := {{x}}

Then if you have, for example,

aa = Array[a, {3, 4}] (* {{a[1, 1], a[1, 2], a[1, 3], a[1, 4]}, ...} *)
bb = Array[b, 3] (* {b[1], b[2], b[3]} *)
cc = Array[c, 4] (* {c[1], c[2], c[3], c[4]} *)

you can do

ArrayFlatten[{{aa, col[bb]}, {row[cc], sca[d]}}]

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

while still working with aa, bb, and cc as matrices and vectors.

• This is much more straightforward in that one can "visualize" the construction via the {{aa,bb}} part. However, one has to be careful to construct the vector bb "correctly". The vector cannot simply be bb={1,2,3}, for instance, but rather bb={{1},{2},{3}}. Then again, this isn't bad, as it makes more sense to a beginner for a column vector to be written this way. Sep 22, 2014 at 15:43
• Yes, it doesn't play too well with the usual way vectors are represented in Mathematica; even row vectors have to be {{1, 2, 3}} instead of just {1, 2, 3} for this to work. So it's far from a perfect solution.
– user484
Sep 22, 2014 at 15:51
• It might make sense for your students if you point out that Mathematica generally doesn't distinguish between row and column vectors (what would be x'*A*x in Matlab is just x.a.x in Mathematica). Only in the context of ArrayFlatten does it matter, so you could define functions row[v_] := {v} and col[v_] := {#}& /@ v (and sca[x_] := {{x}}) to put things into the right "shape" for it.
– user484
Sep 22, 2014 at 15:56
• This seems to be one of the features of Mathematica that is both a "blessing and a curse." It is extremely powerful and convenient to write things like x.A.x. But then putting a column vector in the "right shape" as you mention to use for matrix augmenting then puts the vector in the "wrong shape" for things like A.x. I'll mark your answer as correct as it is the most intuitive I've seen, and perhaps the best considering Mathematica's limitations in this regard. Sep 22, 2014 at 16:03
• @GregH: Please see my edit for an example of what I have in mind.
– user484
Sep 22, 2014 at 19:11

I would do the job with Joincommand.

am=Table[Alphabet[][[j+d(i-1)]],{i,1,3},{j,1,3}];
MatrixForm[am]
bm=Table[{Alphabet[][[i]]},{i,21,23}];
MatrixForm[bm]
cm=Transpose[Join[Transpose[am],Transpose[bm]]];
MatrixForm[cm]

• This is way too long for being seen as "intuitive". (As described by OP.) Apr 5, 2018 at 15:12
• @AntonAntonov cm= is the part GregH needs. I dont think it is not intuitive. Just note that Join glues a martix at the bottom of the other. Using Transpose we can glue them side by side... Apr 5, 2018 at 21:14