---
title: "IdentityMatrix"
language: "en"
type: "Symbol"
summary: "IdentityMatrix[n] gives the n*n identity matrix. IdentityMatrix[{m, n}] gives the m*n identity matrix."
keywords: 
- identity
- identity array
- unit array
- unit diagonal
- unit matrix
- IDENTITY
- identity
- eye
canonical_url: "https://reference.wolfram.com/language/ref/IdentityMatrix.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Constructing Matrices"
    link: "https://reference.wolfram.com/language/guide/ConstructingMatrices.en.md"
  - 
    title: "Structured Arrays"
    link: "https://reference.wolfram.com/language/guide/StructuredArrays.en.md"
  - 
    title: "Matrices and Linear Algebra"
    link: "https://reference.wolfram.com/language/guide/MatricesAndLinearAlgebra.en.md"
  - 
    title: "Geometric Transforms"
    link: "https://reference.wolfram.com/language/guide/GeometricTransforms.en.md"
  - 
    title: "Linear and Nonlinear Filters"
    link: "https://reference.wolfram.com/language/guide/LinearAndNonlinearFilters.en.md"
  - 
    title: "Structure Matrices & Convolution Kernels"
    link: "https://reference.wolfram.com/language/guide/StructureMatricesAndConvolutionKernels.en.md"
related_functions: 
  - 
    title: "DiagonalMatrix"
    link: "https://reference.wolfram.com/language/ref/DiagonalMatrix.en.md"
  - 
    title: "KroneckerDelta"
    link: "https://reference.wolfram.com/language/ref/KroneckerDelta.en.md"
  - 
    title: "UnitVector"
    link: "https://reference.wolfram.com/language/ref/UnitVector.en.md"
  - 
    title: "PermutationMatrix"
    link: "https://reference.wolfram.com/language/ref/PermutationMatrix.en.md"
  - 
    title: "Table"
    link: "https://reference.wolfram.com/language/ref/Table.en.md"
related_tutorials: 
  - 
    title: "Vectors and Matrices"
    link: "https://reference.wolfram.com/language/tutorial/Lists.en.md#2534"
  - 
    title: "Constructing Matrices"
    link: "https://reference.wolfram.com/language/tutorial/LinearAlgebra.en.md#10712"
---
# IdentityMatrix

IdentityMatrix[n] gives the n\[Cross]n identity matrix. 

IdentityMatrix[{m, n}] gives the m\[Cross]n identity matrix.

## Details and Options

* The identity matrix is the identity element for the multiplication of square matrices.

* The entries of the identity matrix $\mathcal{I}$ are given by $\mathcal{I}[[i,j]]=\delta _{i,j}$; that is, one for main diagonal entries and zeros elsewhere.

[image]

* The ``n``\[Cross]``n`` identity matrix ``ℐ`` satisfies the relation ``m.ℐ = ℐ.m = m`` for any ``n``\[Cross]``n`` matrix ``m``.

* The ``n``\[Cross]``n`` identity matrix is symmetric, positive definite and unitary, while the ``m``\[Cross]``n`` identity matrix is unitary.

* ``IdentityMatrix`` by default creates a matrix containing exact integers.

* ``IdentityMatrix[…, SparseArray]`` gives the identity matrix as a ``SparseArray`` object.

* The following options can be given:

|                   |           |                                      |
| ----------------- | --------- | ------------------------------------ |
| TargetStructure   | Automatic | the structure of the returned matrix |
| WorkingPrecision  | Infinity  | precision at which to create entries |

* Possible settings for ``TargetStructure`` include:

|              |                                                  |
| ------------ | ------------------------------------------------ |
| Automatic    | automatically choose the representation returned |
| "Dense"      | represent the matrix as a dense matrix           |
| "Hermitian"  | represent the matrix as a Hermitian matrix       |
| "Orthogonal" | represent the matrix as an orthogonal matrix     |
| "Sparse"     | represent the matrix as a sparse array           |
| "Structured" | represent the matrix as a structured array       |
| "Symmetric"  | represent the matrix as a symmetric matrix       |
| "Unitary"    | represent the matrix as a unitary matrix         |

* With the setting ``TargetStructure -> Automatic``, a dense matrix is returned if the number of matrix entries is less than a preset threshold, and a structured array is returned otherwise.

* Identity matrices, when represented as structured arrays, allow for efficient storage and more efficient operations, including ``Det``, ``Dot``, ``Inverse`` and ``LinearSolve``.

* Operations that are accelerated for ``IdentityMatrix`` include:

|             |                                           |
| ----------- | ----------------------------------------- |
| Det         | time $O(1)$ |
| Dot         | time $O(1)$ |
| Inverse     | time $O(1)$ |
| LinearSolve | time $O(1)$ |

* For a structured ``IdentityMatrix`` ``id``, the following properties ``"prop"`` can be accessed as ``id["prop"]`` :

|                        |                                                                 |
| ---------------------- | --------------------------------------------------------------- |
| "WorkingPrecision"     | precision used internally                                       |
| "Properties"           | list of supported properties                                    |
| "Structure"            | type of structured array                                        |
| "StructuredData"       | internal data stored by the structured array                    |
| "StructuredAlgorithms" | list of functions with special methods for the structured array |
| "Summary"              | summary information, represented as a Dataset                   |

* ``Normal[IdentityMatrix[…]]`` gives the identity matrix as an ordinary matrix.

---

## Examples (33)

### Basic Examples (2)

Construct a 3×3 identity matrix:

```wl
In[1]:= IdentityMatrix[3]//MatrixForm

Out[1]//MatrixForm=
(⁠|   |   |   |
| - | - | - |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |⁠)
```

Visualize the matrix:

```wl
In[2]:= MatrixPlot[%]

Out[2]= [image]
```

---

Create a 2×3 identity matrix:

```wl
In[1]:= IdentityMatrix[{2, 3}]//MatrixForm

Out[1]//MatrixForm=
(⁠|   |   |   |
| - | - | - |
| 1 | 0 | 0 |
| 0 | 1 | 0 |⁠)
```

### Scope (6)

A square identity matrix:

```wl
In[1]:= IdentityMatrix[4]//MatrixForm

Out[1]//MatrixForm=
(⁠|   |   |   |   |
| - | - | - | - |
| 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 |⁠)
```

---

A rectangular identity matrix:

```wl
In[1]:= MatrixForm@IdentityMatrix[{5, 6}]

Out[1]//MatrixForm=
(⁠|   |   |   |   |   |   |
| - | - | - | - | - | - |
| 1 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 | 1 | 0 |⁠)
```

---

Compute the rank of an identity matrix:

```wl
In[1]:= MatrixRank[IdentityMatrix[{2, 13}]]

Out[1]= 2
```

---

Construct a sparse identity matrix using the option setting ``TargetStructure -> "Sparse"`` :

```wl
In[1]:= ℐ = IdentityMatrix[1000, TargetStructure -> "Sparse"]

Out[1]=
SparseArray[Automatic, {1000, 1000}, 0, 
 {1, {CompressedData["«2039»"], CompressedData["«2047»"]}, 
  CompressedData["«50»"]}]
```

The sparse representation saves a significant amount of memory for larger matrices:

```wl
In[2]:= {ByteCount[ℐ], ByteCount[Normal[ℐ]]}

Out[2]= {25072, 8000208}
```

---

Generate a structured identity matrix using the option setting ``TargetStructure -> "Structured"`` :

```wl
In[1]:= ℐ = IdentityMatrix[10^4, TargetStructure -> "Structured"]

Out[1]= IdentityMatrix[StructuredArray`StructuredData[{10000, 10000}, {Infinity}]]
```

The representation and computation are efficient for the structured array:

```wl
In[2]:= ℐ//ByteCount

Out[2]= 392

In[3]:= Det[ℐ]//AbsoluteTiming

Out[3]= {0.0038678, 1}

In[4]:= Inverse[ℐ]//ByteCount//AbsoluteTiming

Out[4]= {0.000045, 392}
```

The normal representation is dramatically bigger and slower:

```wl
In[5]:= (inormal = Normal@ℐ)//ByteCount//AbsoluteTiming

Out[5]= {0.13783, 800000208}

In[6]:= Det[inormal]//AbsoluteTiming

Out[6]= {143.974, 1}
```

The inverse needs a large amount of storage:

```wl
In[7]:= Inverse[inormal]//ByteCount//AbsoluteTiming

Out[7]= {36.7741, 2400880080}
```

---

``IdentityMatrix`` objects include properties that give information about the array:

```wl
In[1]:= im = IdentityMatrix[9, TargetStructure -> "Structured"]

Out[1]= IdentityMatrix[StructuredArray`StructuredData[{9, 9}, {Infinity}]]

In[2]:= im["Properties"]

Out[2]= {"WorkingPrecision", "Properties", "Structure", "StructuredData", "StructuredAlgorithms", "Summary"}
```

``"WorkingPrecision"`` gives the precision of the matrix entries:

```wl
In[3]:= im["WorkingPrecision"]

Out[3]= ∞
```

The ``"Summary"`` property gives a brief summary of information about the array:

```wl
In[4]:= im["Summary"]

Out[4]= Dataset[Association["Structure" -> IdentityMatrix, "Dimensions" -> {9, 9}]]
```

The ``"StructuredAlgorithms"`` property lists the functions that have structured algorithms:

```wl
In[5]:= im["StructuredAlgorithms"]

Out[5]= {CholeskyDecomposition, Conjugate, ConjugateTranspose, Det, Diagonal, Dot, Eigensystem, Eigenvalues, Eigenvectors, Inverse, KroneckerProduct, LeastSquares, LinearSolve, LUDecomposition, MatrixExp, MatrixLog, MatrixPower, MatrixRank, Norm, PseudoInverse, QRDecomposition, SingularValueDecomposition, SingularValueList, Times, Tr, Transpose}
```

### Options (7)

#### TargetStructure (5)

Return the identity matrix as a dense matrix:

```wl
In[1]:= IdentityMatrix[3, TargetStructure -> "Dense"]

Out[1]= {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}
```

---

Return the identity matrix as a structured array:

```wl
In[1]:= IdentityMatrix[3, TargetStructure -> "Structured"]

Out[1]= IdentityMatrix[StructuredArray`StructuredData[{3, 3}, {Infinity}]]
```

---

Return the identity matrix as a sparse array:

```wl
In[1]:= IdentityMatrix[3, TargetStructure -> "Sparse"]

Out[1]= SparseArray[Automatic, {3, 3}, 0, {1, {{0, 1, 2, 3}, {{1}, {2}, {3}}}, {1, 1, 1}}]
```

---

With the setting ``TargetStructure -> Automatic``, a dense matrix is returned for small dimensions:

```wl
In[1]:= IdentityMatrix[5, TargetStructure -> Automatic]

Out[1]= {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 0, 1}}
```

For large dimensions, a structured representation is returned:

```wl
In[2]:= IdentityMatrix[10000, TargetStructure -> Automatic]

Out[2]= IdentityMatrix[StructuredArray`StructuredData[{10000, 10000}, {Infinity}]]
```

---

The dense representation uses a lot of memory for large lists:

```wl
In[1]:= IdentityMatrix[10000, TargetStructure -> "Dense"] //ByteCount

Out[1]= 800000208
```

The sparse representation typically uses less memory:

```wl
In[2]:= IdentityMatrix[10000, TargetStructure -> "Sparse"] //ByteCount

Out[2]= 240792
```

The structured representation uses even less memory:

```wl
In[3]:= IdentityMatrix[10000, TargetStructure -> "Structured"] //ByteCount

Out[3]= 392
```

#### WorkingPrecision (2)

Create a machine-precision identity matrix:

```wl
In[1]:= IdentityMatrix[3, WorkingPrecision -> MachinePrecision]

Out[1]= {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}
```

---

Create an identity matrix with ones of precision 24:

```wl
In[1]:= IdentityMatrix[2, WorkingPrecision -> 24]

Out[1]= {{1.00000000000000000000000, 0}, {0, 1.00000000000000000000000}}
```

### Applications (3)

Use ``IdentityMatrix`` to quickly define the standard basis on $\mathbb{R}^n$ :

```wl
In[1]:= {i, j, k} = IdentityMatrix[3];
```

The variables $i$, $j$ and $k$ can now be used as the standard basis variables:

```wl
In[2]:= j\[Cross]i == -k

Out[2]= True
```

---

Compute the characteristic polynomial using ``IdentityMatrix`` :

```wl
In[1]:=
m = (⁠|   |   |   |
| - | - | - |
| 5 | 6 | 1 |
| 3 | 1 | 8 |
| 4 | 2 | 3 |⁠);

In[2]:= Det[m - λ IdentityMatrix[3]]

Out[2]= 75 + 15 λ + 9 λ^2 - λ^3
```

Compare with a direct computation using ``CharacteristicPolynomial`` :

```wl
In[3]:= CharacteristicPolynomial[m, λ]

Out[3]= 75 + 15 λ + 9 λ^2 - λ^3
```

---

Form the augmented matrix that combines a matrix ``m`` with the identity matrix:

```wl
In[1]:=
m = (⁠|    |    |    |
| -- | -- | -- |
| -2 | 6  | 7  |
| -9 | 4  | -4 |
| 0  | -2 | -3 |⁠);

In[2]:= (a = Join[m, IdentityMatrix[3], 2])//MatrixForm

Out[2]//MatrixForm=
(⁠|    |    |    |   |   |   |
| -- | -- | -- | - | - | - |
| -2 | 6  | 7  | 1 | 0 | 0 |
| -9 | 4  | -4 | 0 | 1 | 0 |
| 0  | -2 | -3 | 0 | 0 | 1 |⁠)
```

Row reduction of the augmented matrix gives an identity matrix augmented with ``Inverse[m]`` :

```wl
In[3]:= (r = RowReduce[a])//MatrixForm

Out[3]//MatrixForm=
(⁠|   |   |   |         |       |         |
| - | - | - | ------- | ----- | ------- |
| 1 | 0 | 0 | -5      | 1     | -13     |
| 0 | 1 | 0 | -(27/4) | (3/2) | -(71/4) |
| 0 | 0 | 1 | (9/2)   | -1    | (23/2)  |⁠)
```

Verify that the right half of ``r`` truly is ``Inverse[m]`` :

```wl
In[4]:=
inv = r[[All, -3 ;; ]];
m.inv == inv.m == IdentityMatrix[3]

Out[4]= True
```

### Properties & Relations (15)

The determinant of a square identity matrix is always 1:

```wl
In[1]:= Det[IdentityMatrix[22]]

Out[1]= 1
```

---

For an ``n\[Cross]m`` matrix ``ℳ``, ``Tr[ℳ] == Min[n, m]`` :

```wl
In[1]:= Tr[IdentityMatrix[{3, 5}]]

Out[1]= 3
```

---

A square identity matrix is its own inverse and its own transpose:

```wl
In[1]:=
id = IdentityMatrix[4];
id == Inverse[id] == Transpose[id]

Out[1]= True
```

---

The scalar multiple of an identity matrix is a diagonal matrix:

```wl
In[1]:= DiagonalMatrixQ[α IdentityMatrix[3]]

Out[1]= True
```

---

The $i$$$^{\text{th}}$$, $j$$$^{\text{th}}$$ entry of any identity matrix is given by ``KroneckerDelta[i, j]`` :

```wl
In[1]:=
m = 3;n = 5;
id = IdentityMatrix[{m, n}];
i = RandomInteger[{1, m}];
j = RandomInteger[{1, n}];
id[[i, j]] == KroneckerDelta[i, j]

Out[1]= True
```

---

The $i$$$^{\text{th}}$$ row or column of ``IdentityMatrix[n]`` is ``UnitVector[n, i]`` :

```wl
In[1]:=
Block[{n = 4}, 
	id = IdentityMatrix[n];
	Table[id[[i]] == UnitVector[n, i] == id[[All, i]], {i, n}]]

Out[1]= {True, True, True, True}
```

For ``IdentityMatrix[{n, m}]``, the rows are instead ``UnitVector[m, i]`` for ``i ≤ Min[n, m]`` :

```wl
In[2]:=
Block[{n = 3, m = 5}, 
	id = IdentityMatrix[{n, m}];
	Table[id[[i]] == UnitVector[m, i] && id[[All, i]] == UnitVector[n, i], {i, Min[n, m]}]]

Out[2]= {True, True, True}
```

---

``IdentityMatrix`` can be converted to a structured ``DiagonalMatrix`` :

```wl
In[1]:= DiagonalMatrix[IdentityMatrix[4]]

Out[1]= DiagonalMatrix[StructuredArray`StructuredData[{4, 4}, {{1, 1, 1, 1}, 0}]]

In[2]:= DiagonalMatrix[IdentityMatrix[{4, 2}]]

Out[2]= DiagonalMatrix[StructuredArray`StructuredData[{4, 2}, {{1, 1}, 0}]]
```

---

Use ``DiagonalMatrix`` for general diagonal matrices:

```wl
In[1]:= DiagonalMatrix[{a, b, c}]

Out[1]= {{a, 0, 0}, {0, b, 0}, {0, 0, c}}
```

---

For an invertible ``n×n`` matrix ``m``, ``Inverse[m].m == m.Inverse[m] == IdentityMatrix[n]`` :

```wl
In[1]:=
m = (⁠|   |    |   |
| - | -- | - |
| 2 | 3  | 5 |
| 4 | 10 | 7 |
| 3 | 6  | 7 |⁠);

In[2]:= Inverse[m].m  == m . Inverse[m]  == IdentityMatrix[3]

Out[2]= True
```

---

For an ``n×m`` matrix ``a``, ``a.PseudoInverse[a] == IdentityMatrix[n]`` :

```wl
In[1]:= a = HilbertMatrix[{3, 4}]

Out[1]= {{1, (1/2), (1/3), (1/4)}, {(1/2), (1/3), (1/4), (1/5)}, {(1/3), (1/4), (1/5), (1/6)}}

In[2]:= a.PseudoInverse[a] == IdentityMatrix[3]

Out[2]= True
```

---

The pseudoinverse of an identity matrix is its transpose:

```wl
In[1]:=
id = IdentityMatrix[{7, 5}];
PseudoInverse[id] == Transpose[id]

Out[1]= True
```

---

For a nonsingular ``n×n`` matrix ``m``, ``MatrixPower[m, 0] == IdentityMatrix[n]`` :

```wl
In[1]:=
m = (⁠|   |    |   |
| - | -- | - |
| 2 | 3  | 5 |
| 4 | 10 | 7 |
| 3 | 6  | 7 |⁠);

In[2]:= MatrixPower[m, 0] == IdentityMatrix[3]

Out[2]= True
```

---

The ``KroneckerProduct`` of a matrix with the identity matrix is a block diagonal matrix:

```wl
In[1]:= a = {{1, 2}, {3, 4}};

In[2]:= KroneckerProduct[IdentityMatrix[3, TargetStructure -> "Structured"], a]

Out[2]=
BlockDiagonalMatrix[StructuredArray`StructuredData[{6, 6}, 
  {{{{1, 2}, {3, 4}}, {{1, 2}, {3, 4}}, {{1, 2}, {3, 4}}}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6}}]]

In[3]:= MatrixForm[%]

Out[3]//MatrixForm=
(⁠|   |   |   |   |   |   |
| - | - | - | - | - | - |
| 1 | 2 | 0 | 0 | 0 | 0 |
| 3 | 4 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 2 | 0 | 0 |
| 0 | 0 | 3 | 4 | 0 | 0 |
| 0 | 0 | 0 | 0 | 1 | 2 |
| 0 | 0 | 0 | 0 | 3 | 4 |⁠)
```

---

The ``WorkingPrecision`` option is equivalent to creating the matrix, then applying ``N`` :

```wl
In[1]:= IdentityMatrix[3, WorkingPrecision -> MachinePrecision]

Out[1]= {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}

In[2]:= N[IdentityMatrix[3]]

Out[2]= {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}
```

---

If ``IdentityMatrix`` is a square matrix, it can be converted to a ``PermutationMatrix`` :

```wl
In[1]:= PermutationMatrix[IdentityMatrix[4]]

Out[1]= PermutationMatrix[StructuredArray`StructuredData[{4, 4}, {Cycles[{}], Infinity}]]
```

Get the cycle representation of the identity permutation:

```wl
In[2]:= %["PermutationCycles"]

Out[2]= Cycles[{}]
```

## See Also

* [`DiagonalMatrix`](https://reference.wolfram.com/language/ref/DiagonalMatrix.en.md)
* [`KroneckerDelta`](https://reference.wolfram.com/language/ref/KroneckerDelta.en.md)
* [`UnitVector`](https://reference.wolfram.com/language/ref/UnitVector.en.md)
* [`PermutationMatrix`](https://reference.wolfram.com/language/ref/PermutationMatrix.en.md)
* [`Table`](https://reference.wolfram.com/language/ref/Table.en.md)

## Tech Notes

* [Vectors and Matrices](https://reference.wolfram.com/language/tutorial/Lists.en.md#2534)
* [Constructing Matrices](https://reference.wolfram.com/language/tutorial/LinearAlgebra.en.md#10712)

## Related Guides

* [Constructing Matrices](https://reference.wolfram.com/language/guide/ConstructingMatrices.en.md)
* [Structured Arrays](https://reference.wolfram.com/language/guide/StructuredArrays.en.md)
* [Matrices and Linear Algebra](https://reference.wolfram.com/language/guide/MatricesAndLinearAlgebra.en.md)
* [Geometric Transforms](https://reference.wolfram.com/language/guide/GeometricTransforms.en.md)
* [Linear and Nonlinear Filters](https://reference.wolfram.com/language/guide/LinearAndNonlinearFilters.en.md)
* [Structure Matrices & Convolution Kernels](https://reference.wolfram.com/language/guide/StructureMatricesAndConvolutionKernels.en.md)

## History

* Introduced in 1988 (1.0) \| [Updated in 2008 (7.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn70.en.md) ▪ [2023 (13.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn133.en.md) ▪ [2024 (14.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn140.en.md)