Written and maintained by CASRAI Editorial Board
Last updated
Short answer: MATLAB has no function called unit or unitmatrix. The “unit matrix” is the identity matrix (ones on the main diagonal, zeros elsewhere), and you create it with eye(n). For a sparse version use speye(n). Source: MathWorks documentation for eye, read 2026-11-07.
This page is for researchers and analysts who need a unit matrix in MATLAB code and want the exact syntax, the data-type options, and the cases where the result is not what they expected.
What it is used for in research
“Unit matrix” is an older or textbook name for the identity matrix, written I. Multiplying a conformable matrix by I leaves it unchanged (A*I = A), which is why it is the starting point for many numerical routines. Typical uses in analysis code are initialising a matrix before a loop fills it, building a matrix such as A + λ*I from an existing square matrix A, and testing results (for example, checking that a matrix times its inverse is close to the identity). For matrix products in general see matrix multiplication in MATLAB.
Unit matrix, identity matrix, ones and diag are different things
- Unit matrix / identity matrix:
eye(n), ones only on the main diagonal. - All-ones matrix:
ones(n)is a different function and fills every element with 1. It is not the unit matrix. - Diagonal matrix:
diagbuilds a matrix from chosen diagonal values; the identity is the special case where those values are all 1.
Install or access (official source only)
eye is a built-in MATLAB function (documented by MathWorks as available since before R2006a), so no toolbox is needed for the standard forms. Get MATLAB from MathWorks at mathworks.com/products/matlab.html. The 'gpuArray', 'distributed' and 'codistributed' options need the corresponding parallel computing support; the eye documentation describes the GPU case under Parallel Computing Toolbox.
Step-by-step: create an identity (unit) matrix
- Square n-by-n matrix. Type
I = eye(4). MATLAB returns a 4-by-4 matrix with ones on the main diagonal and zeros elsewhere. - Non-square n-by-m matrix. Type
I = eye(2,3), or pass a size vector:eye([2,3]). The result is 2-by-3 with the same diagonal pattern. - Match the size of an existing matrix. Type
I = eye(size(A)). - Choose a data type. Add a type name as the last argument:
eye(5,"int8")oreye(3,"uint32"). The default is"double". The accepted names are"double","single","logical","int8","uint8","int16","uint16","int32","uint32","int64"and"uint64". - Match another array’s type, sparsity and complexity. Use
like=:eye(size(p),like=p). If p is sparse the result is sparse; if p is complex the result is complex. - Sparse identity. For large matrices that are mostly zeros, use
speye(n), which returns a sparse n-by-n identity matrix (speye documentation, read 2026-11-07).speye(n,m),speye(sz)andspeye(a sparse scalar 1) also work; a type-name argument was added in R2025a.
Quick syntax reference
| Call | Result |
|---|---|
eye |
Scalar 1 |
eye(n) |
n-by-n identity matrix |
eye(n,m) or eye([n,m]) |
n-by-m matrix, ones on the main diagonal |
eye(___,typename) |
Same, in the named data type |
eye(___,like=p) |
Same, matching type, sparsity and complexity of p |
speye(n) |
Sparse n-by-n identity matrix |
Output and how to read it
For eye(3) the display is a 3-by-3 array with 1 in positions (1,1), (2,2) and (3,3) and 0 everywhere else. For a non-square request such as eye([3,1]) the result is a 3-by-1 column: a 1 followed by zeros, because only positions where the row and column index match carry a 1. Multiplying any matrix A by an identity matrix of conforming size returns A, which gives a quick sanity check: isequal(A*eye(size(A,2)), A) should be true for numeric A.
A common pattern is adding a scaled identity to a square matrix, written A + lambda*eye(size(A)); this adds lambda to each diagonal element and leaves the others unchanged. If you later transpose or reshape results, see transpose in MATLAB.
Troubleshooting
- Empty result. If n or m is 0 or negative, the output is empty; negative dimensions are treated as 0 (MathWorks eye documentation, read 2026-11-07).
- Not square.
eye(2,3)is allowed and is not a true identity matrix; it only has the diagonal pattern. Useeye(n)when you need I for products or inverses. - Unexpected integer behaviour. An integer type such as
"int8"or"uint8"gives integer storage, so later arithmetic follows integer rules. Use the default"double"unless you need a specific type. - Sparse versus full.
eye(n)returns a full matrix; usespeye(n)orlike=with a sparse array to keep it sparse. - “Undefined function” for unit or unitmatrix. No such function exists; use
eye.
Free alternatives
| Tool | Command | Notes (source) |
|---|---|---|
| GNU Octave | eye(n), eye(m,n), eye([m,n]), eye(...,class) |
Default class is double; eye(2,"uint8") gives a 2-by-2 uint8 matrix; negative dimensions are treated as zero. Octave also provides speye. Source: Octave manual, Special Utility Matrices, read 2026-11-07. |
For other statistical and numerical software, see the research tools hub. MATLAB also has other data containers, covered in structures in MATLAB.
FAQ
What is the unit matrix in MATLAB?
It is the identity matrix, created with eye(n). MATLAB has no function named unit.
How do I make a unit matrix of a given size?
eye(n) for n-by-n, or eye(n,m) for n-by-m.
Is ones(n) the unit matrix?
No. ones(n) fills every element with 1; the identity has ones only on the main diagonal.
Can I make an identity matrix of integers?
Yes: eye(3,"int8") or any of the type names listed above.
Is there a sparse identity matrix?
Yes: speye(n).
Sources (official documentation)
- MathWorks, eye: Identity matrix, read 2026-11-07.
- MathWorks, speye: Sparse identity matrix, read 2026-11-07.
- GNU Octave manual, Special Utility Matrices, read 2026-11-07.







