Function Repository Resource:

MatrixAugmentLeft

Source Notebook

Augment a matrix on the left by prepending each row with a corresponding entry from a list or with a constant

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

ResourceFunction["MatrixAugmentLeft"][matrix,list]

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

ResourceFunction["MatrixAugmentLeft"][matrix,s]

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

ResourceFunction["MatrixAugmentLeft"][leading]

represents an operator form of ResourceFunction["MatrixAugmentLeft"] that, applied to a matrix, augments it on the left with leading.

Details

ResourceFunction["MatrixAugmentLeft"][matrix,list] 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 prepended to the i-th row.
When the second argument is a scalar, the same scalar is prepended to every row.
An operator form is provided. ResourceFunction["MatrixAugmentLeft"][x] returns a function that, applied to a matrix, performs the augmentation; this composes naturally with //, Composition, and Fold.
MatrixAugmenLeft 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["MatrixAugmentLeft"] requires an actual matrix input and specifically it does not support ragged arrays.

Examples

Basic Examples (3) 

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

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

Display in matrix form to see the leading column:

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

Augment with a constant scalar. The same value is prepended to every row:

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

Scope (3) 

Use the operator form for pipeline composition:

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

MatrixAugmentLeft works with non-numeric matrices:

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

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

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

Applications (4) 

Row Labeling and Ordering (1) 

Use MatrixAugmentLeft to append row identifiers to a data matrix by left augmentation, enabling recovery of original row positions after reordering:

In[7]:=
MatrixForm[
 	With[
  		{
   			obs = {
     				{2.1, 3.4, 0.7},
     				{5.2, 1., 8.3},
     				{4.5, 7.7, 2.2},
     				{0.4, 2.9, 6.1}
     			}
   		},
  		ResourceFunction["MatrixAugmentLeft"][Range @ Length @ obs]@obs
  	]
 ]
Out[7]=

The operator form provides a direct alternative to transposition-based constructions for row labeling.

Time-Stamped Trajectories (1) 

Use MatrixAugmentLeft to construct a time-stamped trajectory matrix, where each row is a state observation and the leading column carries the sample time:

In[8]:=
MatrixForm[
 	With[
  		{
   			times = Range[0, 1, 1 / 4],
   			trajectory = Table[
     				{Sin @ t, Cos @ t, Sin[2 * t]},
     				{t, 0, 1, 1 / 4}
     			]
   		},
  		N @ ResourceFunction["MatrixAugmentLeft"][times][trajectory]
  	]
 ]
Out[8]=

Cofactor Sign Tables (1) 

Construct a cofactor sign table for Laplace expansion by prepending the row-wise cofactor signs:

In[9]:=
MatrixForm[
 	With[
  		{
   			M = {{2, -1, 3}, {4, 5, -2}, {1, 0, 7}},
   			signs = {1, -1, 1}
   		},
  		ResourceFunction["MatrixAugmentLeft"][signs][M]
  	]
 ]
Out[9]=

Boundary Extension via Augmentation (1) 

Use MatrixAugmentLeft to extend a matrix by replicating its boundary values via left augmentation:

In[10]:=
extendLeft[m_] := ResourceFunction["MatrixAugmentLeft"][m[[All, 1]]][m];
In[11]:=
MatrixForm @ extendLeft[{{1, 2}, {3, 4}}]
Out[11]=

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

Properties and Relations (5) 

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

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

MatrixAugmentLeft is equivalent to a construction via outer transposition using Transpose and Prepend:

In[13]:=
With[
 	{
  		A = {{1, 2}, {3, 4}},
  		b = {2, 3}
  	},
 	SameQ[ResourceFunction["MatrixAugmentLeft"][A, b], Transpose[Prepend[Transpose[A], b]]]
 ]
Out[13]=

MatrixAugmentLeft is equivalent to a row-wise construction using MapThread with Prepend:

In[14]:=
With[
 	{
  		A = {{1, 2}, {3, 4}},
  		b = {2, 3}
  	},
 	SameQ[ResourceFunction["MatrixAugmentLeft"][A, b], MapThread[Prepend, {A, b}]]
 ]
Out[14]=

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

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

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

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

Unlike PrependColumn, MatrixAugmentLeft 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[17]:=
ResourceFunction["MatrixAugmentLeft"][{{1, 2}, {3, 4}}, {5, 6, 7}]
Out[17]=

Neat Examples (3) 

Build a labeled correlation matrix where the leading column contains the variable names. Each row i shows variable xi followed by its correlations with all variables:

In[18]:=
With[
 	{
  		data = RandomReal[NormalDistribution[], {100, 3}],
  		names = {"x", "y", "z"}
  	},
 	Function[
   		Grid[#, Spacings -> {1, 1}, Frame -> All, Alignment -> Left]
   	][ResourceFunction["MatrixAugmentLeft"][names][Correlation @ data]]
 ]
Out[18]=

Construct the bordered Hessian for the quadratic objective under the linear constraint gTX=c. The sign of the bordered determinant determines the type of constrained extremum:

In[19]:=
With[
 	{
  		H = {{2, 1}, {1, 3}},
  		g = {1, 1}
  	},
 	Det[
  		Plus[PadLeft[ResourceFunction["MatrixAugmentLeft"][g][H], {3, 3}],
   			SparseArray[{{1, 2} -> 1, {1, 3} -> 1},
    				{3, 3}
    			]
   		]
  	]
 ]
Out[19]=

A negative bordered determinant indicates that the constrained critical point is a maximum of f on the constraint manifold, consistent with second-order sufficient conditions for constrained optimization.


Use MatrixAugmentLeft 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[20]:=
extendLeft[m_] := ResourceFunction["MatrixAugmentLeft"][m, m[[All, 1]]];

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

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

extendBottom[m_] := Transpose@Reverse[extendLeft[Reverse[Transpose[m], 2]], 2];
In[21]:=
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,
      {extendLeft, extendTop, extendRight, 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[21]=

This constructs a boundary-extended matrix by applying directional augmentations. Each boundary is generated by rotating the matrix and reusing left-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

MatrixAugmentLeft constructs the left augmentation of a matrix by prepending 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 dual form of the classical augmented matrix construction in linear algebra, commonly used when leading columns carry structural or labeling information. It admits multiple equivalent formulations, including block matrix construction with ArrayFlatten, row-wise extension with MapThread, and transposition-based formulations. Beyond its standard role, MatrixAugmentLeft serves as a structural operator for prefixing matrices. It is particularly suited for attaching identifiers, constructing labeled datasets, forming time-stamped trajectories, generating cofactor sign tables, and performing controlled boundary extensions in discretized domains. This makes it applicable across symbolic computation, data transformation, and algebraic constructions.

License Information