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?

  • $\begingroup$ Are you aware of this thread: Elegant operations on matrix rows and columns? Might be a duplicate at that... $\endgroup$ – Yves Klett Sep 22 '14 at 15:08
  • $\begingroup$ 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. $\endgroup$ – GregH Sep 22 '14 at 15:24
  • 1
    $\begingroup$ 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. $\endgroup$ – Daniel Lichtblau Sep 22 '14 at 16:21

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.

  • $\begingroup$ 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. $\endgroup$ – GregH Sep 22 '14 at 15:43
  • $\begingroup$ 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. $\endgroup$ – user484 Sep 22 '14 at 15:51
  • 2
    $\begingroup$ 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. $\endgroup$ – user484 Sep 22 '14 at 15:56
  • $\begingroup$ 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. $\endgroup$ – GregH Sep 22 '14 at 16:03
  • $\begingroup$ @GregH: Please see my edit for an example of what I have in mind. $\endgroup$ – user484 Sep 22 '14 at 19:11

I would do the job with Joincommand.


enter image description here

  • $\begingroup$ This is way too long for being seen as "intuitive". (As described by OP.) $\endgroup$ – Anton Antonov Apr 5 '18 at 15:12
  • $\begingroup$ @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... $\endgroup$ – bkarpuz Apr 5 '18 at 21:14

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.