eye

Identity matrix.

Category: Language Fundamentals > Matrices and Arrays

Syntax

I = eye(n)

n-by-n identity matrix.

I = eye(m, n)

m-by-n matrix with ones on the main diagonal and zeros elsewhere.

I = eye(sz)

Size given as a vector [m n].

I = eye(___, classname)

Result of the given class, such as 'int8' or 'single'.

Description

The identity leaves a matrix unchanged under multiplication, A*eye(n) == A. It is also a quick way to start a matrix of known structure, for instance 3*eye(4) or eye(3) + ones(3).

Non-square sizes are allowed; the ones sit on the main diagonal as far as it goes. eye takes no more than two dimensions.

Input arguments

n, m
Number of rows and columns.
classname
Class of the result; default 'double'.

Output arguments

I
Matrix with ones where the row and column index are equal.

Example

eye(3)
eye(2, 3)
eye(2, 'int8')
Output
ans = 3×3

     1     0     0
     0     1     0
     0     0     1
ans = 2×3

     1     0     0
     0     1     0
ans = 2×2 int8 matrix

   1   0
   0   1

See also

diag ones zeros inv kron