Function Repository Resource:

MatrixAugmentRight

Source Notebook

Augment a matrix on the right by extending each row with a corresponding entry from a list or with a constant

Contributed by: E. Chan-López & Erika Yuridia Flores Chan

ResourceFunction["MatrixAugmentRight"][matrix,list]

extends each row of matrix on the right with the corresponding entry of list, producing a matrix with one additional column.

ResourceFunction["MatrixAugmentRight"][matrix,s]

extends each row of matrix on the right with the scalar s.

ResourceFunction["MatrixAugmentRight"][trailing]

represents the operator form of ResourceFunction["MatrixAugmentRight"] that, applied to a matrix, augments it on the right with trailing.

Details

ResourceFunction["MatrixAugmentRight"][A,v] returns the matrix[v|A] of dimensions m×(n+1), where A has dimensions m×n and v has length m.
The length of list must equal the number of rows of matrix; the i-th entry of list is appended to the i-th row.
When the second argument is a scalar, that scalar is appended to every row.
An operator form is provided. ResourceFunction["MatrixAugmentRight"][x] returns a function that, applied to a matrix, performs the augmentation; this composes naturally with //, Composition, and Fold.
ResourceFunction["MatrixAugmentRight"] will cause sparse arrays to become dense. One can instead preserve sparsity by constructing the result using shifted column indices. The current implementation intentionally favors a direct block-construction formulation.
If list and matrix have different lengths the function returns $Failed and emits a dimension-mismatch message.
ResourceFunction["MatrixAugmentRight"] requires an actual matrix input and specifically it does not support ragged arrays.

Examples

Basic Examples (3) 

Augment a matrix on the right with a list, one entry per row:

In[1]:=
ResourceFunction["MatrixAugmentRight"][{{1, 2}, {3, 4}}, {2, 3}]
Out[1]=

Display in matrix form to see the block structure:

In[2]:=
ResourceFunction[
  "MatrixAugmentRight"][{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}, {2, 4, 6}] // MatrixForm
Out[2]=

Augment with a constant scalar:

In[3]:=
ResourceFunction[
  "MatrixAugmentRight"][{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}, \[Pi]] // MatrixForm
Out[3]=

Scope (3) 

Use the operator form for pipeline composition:

In[4]:=
RandomReal[1, {3, 4}] // ResourceFunction["MatrixAugmentRight"][{1, 2, 3}] // MatrixForm
Out[4]=

MatrixAugmentRight works with non-numeric matrices:

In[5]:=
ResourceFunction["MatrixAugmentRight"][{{a, b}, {c, d}}, {p, q}]
Out[5]=

The operator form composes naturally with itself, allowing successive augmentations within functional pipelines:

In[6]:=
MatrixForm[
 	Composition[
   		ResourceFunction["MatrixAugmentRight"] @ {100, 200, 300},
   		ResourceFunction["MatrixAugmentRight"] @ {10, 20, 30}
   	][IdentityMatrix @ 3]
 ]
Out[6]=

Applications (5) 

Solving Linear Systems (1) 

Use MatrixAugmentRight in operator form to construct the augmented matrix A|b, then apply RowReduce:

In[7]:=
With[
 	{
  		A = {{1, 2}, {3, 4}},
  		b = {2, 3}
  	},
 	MatrixForm @ RowReduce @ ResourceFunction["MatrixAugmentRight"][b][A]
 ]
Out[7]=

The unique solution x=(-1,3/2) is obtained from the last column of the reduced row echelon form.

Design Matrix with Intercept (1) 

Use MatrixAugmentRight to construct homogeneous coordinates for affine transformations. Given a list of points {p1,,pn} in d, the embedding into projective coordinates d+1 is a right augmentation with the constant scalar 1:

In[8]:=
MatrixForm[
 	With[
  		{pts = {{2, 3}, {5, 1}, {4, 7}, {0, 2}}},
  		ResourceFunction["MatrixAugmentRight"][pts, 1]
  	]
 ]
Out[8]=

This is the canonical preprocessing step for the design matrix in least-squares regression with intercept. Each data row {xi,yi} is augmented with a constant 1 so that the intercept term is absorbed into the matrix–vector product X β.

Vandermonde-Like Matrices (1) 

Use MatrixAugmentRight to construct Vandermonde-like matrices. Starting from a column of ones, each successive power of the abscissa vector is appended via the operator form combined with Fold:

In[9]:=
MatrixForm @ With[{
   x = {1, 2, 3, 4}},
  Fold[
   ResourceFunction["MatrixAugmentRight"][#1, #2] &,
   ConstantArray[1, {Length@x, 1}],
   Table[x^k, {k, 1, 3}]
   ]
  ]
Out[9]=

This constructs a Vandermonde-like matrix by successive right-augmentation, appending increasing powers of the abscissa vector.

Matrix Bordering (1) 

Use MatrixAugmentRight to construct matrices by successive right-augmentation of column blocks:

In[10]:=
MatrixForm @ Fold[
  ResourceFunction["MatrixAugmentRight"][#1, #2] &,
  {{a1, a2}, {a3, a4}},
  {{b1, b3}, {b2, b4}}
  ]
Out[10]=

Each iteration appends a new column, illustrating matrix construction as an incremental bordering process.

Boundary Extension via Augmentation (1) 

Use MatrixAugmentRight to extend a matrix by replicating its boundary values via right augmentation:

In[11]:=
extendRight[m_] := ResourceFunction["MatrixAugmentRight"][m, m[[All, -1]]];
In[12]:=
MatrixForm @ extendRight[{{1, 2}, {3, 4}}]
Out[12]=

This appends the last column to the matrix, illustrating how augmentation can be used to construct simple boundary extensions.

Properties and Relations (5) 

MatrixAugmentRight is equivalent to a block construction using ArrayFlatten with a threaded column vector:

In[13]:=
With[
 	{
  		A = {{1, 2}, {3, 4}},
  		b = {2, 3}
  	},
 	SameQ[ResourceFunction["MatrixAugmentRight"][A, b], ArrayFlatten @ {{A, Thread @ {b}}}]
 ]
Out[13]=

MatrixAugmentRight is equivalent to a construction via outer transposition using Transpose and Append:

In[14]:=
With[
 	{
  		A = {{1, 2}, {3, 4}},
  		b = {2, 3}
  	},
 	SameQ[ResourceFunction["MatrixAugmentRight"][A, b], Transpose[Append[Transpose[A], b]]]
 ]
Out[14]=

MatrixAugmentRight is equivalent to a row-wise construction using MapThread with Append:

In[15]:=
With[
 	{
  		A = {{1, 2}, {3, 4}},
  		b = {2, 3}
  	},
 	SameQ[ResourceFunction["MatrixAugmentRight"][A, b], MapThread[Append, {A, b}]]
 ]
Out[15]=

MatrixAugmentRight can also be expressed using Join along the second level after promoting the vector to a one-column matrix:

In[16]:=
With[
 {
  A = {{1, 2}, {3, 4}},
  b = {2, 3}},
 SameQ[
  ResourceFunction["MatrixAugmentRight"][A, b],
  Join[A, Thread[{b}], 2]
  ]
 ]
Out[16]=

ResourceFunction["AppendColumn"] can also be used for this task:

In[17]:=
With[
 {
  A = {{1, 2}, {3, 4}},
  b = {2, 3}},
 SameQ[
  ResourceFunction["MatrixAugmentRight"][A, b],
  ResourceFunction["AppendColumn"][A, b]
  ]
 ]
Out[17]=

Unlike AppendColumn, MatrixAugmentRight also provides an operator form suited for compositional workflows.

Possible Issues (1) 

A length mismatch between the matrix and the vector generates a dimension error:

In[18]:=
ResourceFunction["MatrixAugmentRight"][{{1, 2}, {3, 4}}, {5, 6, 7}]
Out[18]=

Neat Examples (4) 

Solve a parametric family of linear systems via augmentation and row reduction:

In[19]:=
With[{A = {{1, 2}, {3, 4}}},
 Table[
  Last@Last@
    RowReduce@ResourceFunction["MatrixAugmentRight"][{t, 2 t - 1}]@A,
  {t, 0, 3}
  ]
 ]
Out[19]=

For each parameter value, the system is augmented and reduced, and the solution is read from the last column of the reduced row echelon form.


Append row indices to a dataset and sort by a column while retaining original row positions:

In[20]:=
MatrixForm[
 	With[{data = RandomReal[1, {5, 3}]},
  		SortBy[
   ResourceFunction["MatrixAugmentRight"][Range@Length@data]@data, First]
  	]
 ]
Out[20]=

This attaches a unique index to each row, allowing sorting operations while retaining a reference to the original ordering.


Construct a Cayley table by augmenting a group multiplication table with its row labels:

In[21]:=
With[{g = {0, 1, 2, 3}},
 	MatrixForm[
  		ResourceFunction["MatrixAugmentRight"][g][
   Outer[Function[Mod[# + #2, 4]], g, g]]
  	]
 ]
Out[21]=

This constructs the Cayley table of a finite group and appends the corresponding group elements as row labels using right-augmentation.


Use MatrixAugmentRight together with rotations to construct boundary extensions of a matrix. Each side is obtained by directional augmentation, enabling consistent neighborhood structure without explicit padding functions:

In[22]:=
extendRight[m_] := ResourceFunction["MatrixAugmentRight"][m, m[[All, -1]]];

extendLeft[m_] := Reverse[extendRight[Reverse[m, 2]], 2];

extendTop[m_] := Transpose@extendRight[Transpose[m]];

extendBottom[m_] := Transpose@Reverse[extendRight[Reverse[Transpose[m], 2]], 2];
In[23]:=
MatrixForm @ With[{
   orig = {
     {0, 0, 0, 0, 0, 0},
     {0, 0, 0, 0, 1, 0},
     {0, 0, 0, 1, 1, 0},
     {1, 1, 1, 1, 1, 0},
     {0, 0, 0, 1, 1, 0},
     {0, 0, 0, 0, 1, 0},
     {0, 0, 0, 0, 0, 0}
     }
   },
  Module[
   {
    ext = Fold[#2[#1] &, orig, {extendRight, extendLeft, extendTop, extendBottom}],
    r, c
    },
   {r, c} = Dimensions[orig];
   MapIndexed[
    If[
      2 <= #2[[1]] <= r + 1 && 2 <= #2[[2]] <= c + 1
      ,
      Style[#, Red],
      Style[#, Blue]
      ] &,
    ext,
    {2}
    ]
   ]
  ]
Out[23]=

This constructs a boundary-extended matrix by applying directional augmentations. Each boundary is generated by rotating the matrix and reusing right-augmentation, yielding a full extension that preserves local structure.

Publisher

Ramón Eduardo Chan López

Version History

  • 1.0.0 – 08 May 2026

Source Metadata

Related Resources

Author Notes

MatrixAugmentRight constructs the right augmentation of a matrix by appending a vector or scalar as an additional column. The function enforces dimensional consistency when a vector is provided, requiring its length to match the number of rows of the matrix. In operator form, it enables composable workflows by fixing the augmenting data and applying it to multiple matrices. The operation corresponds to the classical construction of an augmented matrix in linear algebra, commonly used in the solution of linear systems via row reduction. It admits multiple equivalent formulations, including block matrix construction with ArrayFlatten, row-wise extension with MapThread, and transposition-based formulations. Beyond its standard role in linear systems, MatrixAugmentRight serves as a general structural operator for extending matrices. It can be used for labeling rows, constructing design matrices, generating polynomial feature expansions, building Cayley tables, and performing controlled boundary extensions in discretized domains. This makes it applicable across symbolic computation, data transformation, and algebraic constructions.

License Information