---
title: "Accessing and Modifying a Matrix"
canonical: "https://docs.magma-maths.org/v2.30-1/Algebras/MatrixAlgebras/operation-element.html"
source: "https://docs.magma-maths.org/v2.30-1/Algebras/MatrixAlgebras/operation-element.md"
part: "Algebras"
chapter: "Matrix Algebras"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Algebras", "source": "https://docs.magma-maths.org/v2.30-1/Algebras/index-algebras.md"}
  - {"title": "Matrix Algebras", "source": "https://docs.magma-maths.org/v2.30-1/Algebras/MatrixAlgebras/index-matrix-algebras.md"}
prev: {"title": "Elementary Operations on Subalgebras and Ideals", "source": "https://docs.magma-maths.org/v2.30-1/Algebras/MatrixAlgebras/subalgebra-ideal.md"}
next: {"title": "Canonical Forms", "source": "https://docs.magma-maths.org/v2.30-1/Algebras/MatrixAlgebras/canonical-form.md"}
intrinsics:
  - {"name": "a", "signature": "a[i]: AlgMatElt, RngIntElt -> ModTupElt"}
  - {"name": "a[i] := u", "signature": "a[i] := u: AlgMatElt, RngIntElt, RngElt -> AlgMatElt"}
  - {"name": "a", "signature": "a[i, j]: AlgMatElt, RngIntElt, RngIntElt -> RngElt"}
  - {"name": "a[i, j] := t", "signature": "a[i, j] := t: AlgMatElt, RngIntElt, RngIntElt, RngElt -> AlgMatElt"}
  - {"name": "ElementToSequence", "signature": "ElementToSequence(a): AlgMatElt -> [ RngElt ]"}
  - {"name": "Eltseq", "signature": "Eltseq(a): AlgMatElt -> [ RngElt ]"}
  - {"name": "Submatrix", "signature": "Submatrix(a, i, j, p, q): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx"}
  - {"name": "ExtractBlock", "signature": "ExtractBlock(a, i, j, p, q): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx"}
  - {"name": "InsertBlock", "signature": "InsertBlock(~a, b, i, j): Mtrx, Mtrx, RngIntElt, RngIntElt -> Mtrx"}
  - {"name": "HorizontalJoin", "signature": "HorizontalJoin(X, Y): Mtrx, Mtrx -> Mtrx"}
  - {"name": "HorizontalJoin", "signature": "HorizontalJoin(Q): [ ModMatRngElt ] -> ModMatRngElt"}
  - {"name": "VerticalJoin", "signature": "VerticalJoin(X, Y): ModMatRngElt, ModMatRngElt -> ModMatRngElt"}
  - {"name": "VerticalJoin", "signature": "VerticalJoin(Q): [ ModMatRngElt ] -> ModMatRngElt"}
  - {"name": "DiagonalJoin", "signature": "DiagonalJoin(X, Y): ModMatRngElt, ModMatRngElt -> ModMatRngElt"}
  - {"name": "DiagonalJoin", "signature": "DiagonalJoin(Q): [ ModMatRngElt ] -> ModMatRngElt"}
  - {"name": "SwapRows", "signature": "SwapRows(~a, i, j): AlgMatElt, RngIntElt, RngIntElt"}
  - {"name": "MultiplyRow", "signature": "MultiplyRow(~a, u, j): AlgMatElt, RngElt, RngIntElt"}
  - {"name": "AddRow", "signature": "AddRow(~a, u, i, j): AlgMatElt, RngElt, RngIntElt, RngIntElt"}
  - {"name": "SwapColumns", "signature": "SwapColumns(~a, i, j): AlgMatElt, RngIntElt, RngIntElt"}
  - {"name": "MultiplyColumn", "signature": "MultiplyColumn(~a, u, i): AlgMatElt, RngElt, RngIntElt"}
  - {"name": "AddColumn", "signature": "AddColumn(~a, u, i, j): AlgMatElt, RngElt, RngIntElt, RngIntElt"}
---

# Accessing and Modifying a Matrix

## Indexing

<a id="indexed-indexed-a-algmatelt-rngintelt-modtupelt"></a>
### `a[i]: AlgMatElt, RngIntElt -> ModTupElt`

Given an element $a$ belonging to the matrix algebra $R$ over the ring $S$, return the $i$-th row of $a$ as an element of the natural $S$-module associated with $R$.

<a id="literal-literal-a-i-u-algmatelt-rngintelt-rngelt-algmatelt"></a>
### `a[i] := u: AlgMatElt, RngIntElt, RngElt -> AlgMatElt`

Given an element $a$ belonging to the matrix algebra $R$ over the ring $S$, an integer $i$ in the range $[1, n]$ and an element $u$ of the natural $S$-module associated with $R$, replace the $i$-th row of $a$ by the vector $u$.

<a id="indexed-indexed-a-algmatelt-rngintelt-rngintelt-rngelt"></a>
### `a[i, j]: AlgMatElt, RngIntElt, RngIntElt -> RngElt`

Given an element $a$ belonging to the matrix algebra $R$ over the ring $S$, return the $(i,j)$-th entry of $a$ as an element of $S$.

<a id="literal-literal-a-i-j-t-algmatelt-rngintelt-rngintelt-rngelt-algmatelt"></a>
### `a[i, j] := t: AlgMatElt, RngIntElt, RngIntElt, RngElt -> AlgMatElt`

Given an element $a$ belonging to the matrix algebra $R$ over the ring $S$, integers $i$ and $j$ in the range $[1, n]$, and an element $t$ of $S$, replace the $(i,j)$-th entry of $a$ by $t$.

<a id="function-elementtosequence-algmatelt"></a>
### `ElementToSequence(a): AlgMatElt -> [ RngElt ]`

<a id="function-eltseq-algmatelt"></a>
### `Eltseq(a): AlgMatElt -> [ RngElt ]`

Given an element $a$ of the matrix algebra $R$ over $S$, where $a = (a_{ij})$, $1 \leq i$, $j \leq n$, return $a$ as the sequence of elements of $S$:

$$
[a_{11}, \ldots, a_{1n}, a_{21},\ldots, a_{2n}, \ldots, a_{n1},\ldots, a_{nn}].
$$

## Extracting and Inserting Blocks

<a id="function-submatrix-mtrx-rngintelt-rngintelt-rngintelt-rngintelt"></a>
### `Submatrix(a, i, j, p, q): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx`

<a id="function-extractblock-mtrx-rngintelt-rngintelt-rngintelt-rngintelt"></a>
### `ExtractBlock(a, i, j, p, q): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx`

Given a matrix $a$ belonging to a subalgebra of $M_n(S)$ and integers $i$, $j$, $p$, and $q$ satisfying the conditions, $1 \le i + p \le m$, $1 \le j + q \le n$, create the matrix $b$ consisting of the $p\times q$ submatrix of $a$ whose first entry is the $(i,j)$-th entry of $a$. If $p \ne q$, the matrix $b$ is created as an element of $\Hom(P, Q)$, where Rank$(P) = p$, Rank$(Q) = q$. Otherwise it is created as an element of $M_p(S)$.

<a id="function-insertblock-mtrx-mtrx-rngintelt-rngintelt-ref"></a>
### `InsertBlock(~a, b, i, j): Mtrx, Mtrx, RngIntElt, RngIntElt -> Mtrx`

(Procedure.) Given that the matrix $a$ belongs to a subalgebra of $M_n(S)$ and the $p\times q$ matrix $b$ is also over $S$, the integers $i$, $j$, $p$, and $q$ must satisfy the conditions, $1 \le i + p \le m$, $1 \le j + q \le n$. This procedure modifies $a$ so that the $p\times q$ block beginning at the $(i,j)$-th entry of $a$ is replaced by $b$.

## Joining Matrices

<a id="function-horizontaljoin-mtrx-mtrx"></a>
### `HorizontalJoin(X, Y): Mtrx, Mtrx -> Mtrx`

Given matrices $X$ with $r$ rows and $c$ columns, and $Y$ with $r$ rows and $d$ columns, both over the same coefficient ring $R$, return the matrix over $R$ with $r$ rows and $(c+d)$ columns obtained by joining $X$ and $Y$ horizontally (placing $Y$ to the right of $X$).

<a id="function-horizontaljoin-modmatrngelt"></a>
### `HorizontalJoin(Q): [ ModMatRngElt ] -> ModMatRngElt`

Given a sequence $Q$ of matrices, each having the same number of rows and being over the same coefficient ring $R$, return the matrix over $R$ obtained by joining the elements of $Q$ horizontally in order.

<a id="function-verticaljoin-modmatrngelt-modmatrngelt"></a>
### `VerticalJoin(X, Y): ModMatRngElt, ModMatRngElt -> ModMatRngElt`

Given matrices $X$ with $r$ rows and $c$ columns and $Y$ with $s$ rows and $c$ columns, both over the same coefficient ring $R$, return the matrix with $(r+s)$ rows and $c$ columns over $R$ obtained by joining $X$ and $Y$ vertically (placing $Y$ underneath $X$).

<a id="function-verticaljoin-modmatrngelt"></a>
### `VerticalJoin(Q): [ ModMatRngElt ] -> ModMatRngElt`

Given a sequence $Q$ of matrices, each having the same number of columns and being over the same coefficient ring $R$, return the matrix over $R$ obtained by joining the elements of $Q$ vertically in order.

<a id="function-diagonaljoin-modmatrngelt-modmatrngelt"></a>
### `DiagonalJoin(X, Y): ModMatRngElt, ModMatRngElt -> ModMatRngElt`

Given matrices $X$ with $a$ rows and $b$ columns and $Y$ with $c$ rows and $d$ columns, both over the same coefficient ring $R$, return the matrix with $(a+c)$ rows and $(b+d)$ columns over $R$ obtained by joining $X$ and $Y$ diagonally (placing $Y$ diagonally to the right of and underneath $X$, with zero blocks above and below the diagonal).

<a id="function-diagonaljoin-modmatrngelt"></a>
### `DiagonalJoin(Q): [ ModMatRngElt ] -> ModMatRngElt`

Given a sequence $Q$ of matrices, each being over the same coefficient ring $R$, return the matrix over $R$ obtained by joining the elements of $Q$ diagonally in order.

## Row and Column Operations

For the following operations, $a$ is an element of a subring of the matrix algebra $M_n(S)$, $u$ is a non-zero element of $S$, and $i$ and $j$ are integers in the range $[1, n]$. Each of the operations described here acts on the matrix in place, and is therefore implemented as a procedure.

<a id="procedure-swaprows-algmatelt-rngintelt-rngintelt-ref"></a>
### `SwapRows(~a, i, j): AlgMatElt, RngIntElt, RngIntElt`

Mutate the matrix $a$ by interchanging rows $i$ and $j$.

<a id="procedure-multiplyrow-algmatelt-rngelt-rngintelt-ref"></a>
### `MultiplyRow(~a, u, j): AlgMatElt, RngElt, RngIntElt`

Mutate the matrix $a$ by multiplying row $j$ by the scalar $u$.

<a id="procedure-addrow-algmatelt-rngelt-rngintelt-rngintelt-ref"></a>
### `AddRow(~a, u, i, j): AlgMatElt, RngElt, RngIntElt, RngIntElt`

Mutate the matrix $a$ by adding $u$ times row $i$ to row $j$.

<a id="procedure-swapcolumns-algmatelt-rngintelt-rngintelt-ref"></a>
### `SwapColumns(~a, i, j): AlgMatElt, RngIntElt, RngIntElt`

Mutate the matrix $a$ by interchanging columns $i$ and $j$.

<a id="procedure-multiplycolumn-algmatelt-rngelt-rngintelt-ref"></a>
### `MultiplyColumn(~a, u, i): AlgMatElt, RngElt, RngIntElt`

Mutate the matrix $a$ by multiplying column $i$ by the scalar $u$.

<a id="procedure-addcolumn-algmatelt-rngelt-rngintelt-rngintelt-ref"></a>
### `AddColumn(~a, u, i, j): AlgMatElt, RngElt, RngIntElt, RngIntElt`

Mutate the matrix $a$ by adding $u$ times column $i$ to column $j$.
