identity

identity(n)

identity(m, n)

identity([m, n])

Try it yourself:


Create an identity matrix

identity(n) creates an n-by-n matrix with ones on the main diagonal and zeros elsewhere. The identity matrix is the multiplicative neutral element of matrix algebra: multiplying a compatible matrix by it leaves that matrix unchanged.

Example and applications

identity(3) creates a 3-by-3 diagonal matrix of ones. It is useful for initializing transforms, constructing linear systems, and expressing formulas such as A + λI. Combine it with multiply to verify the neutral property, zeros to start from an all-zero matrix, and diag to work with diagonals.

Input and shape caveats

n should be a non-negative integer. The identity matrix is square, so its size must match the relevant dimension for multiplication or addition; use size to check. Do not confuse identity with an all-ones matrix, which ones creates.

All functions