# eye

Identity matrix

## Description

I = eye returns the scalar 1.

example

I = eye(n) returns an n-by-n identity matrix with ones on the main diagonal and zeros elsewhere.

example

I = eye(n,m) returns an n-by-m matrix with ones on the main diagonal and zeros elsewhere.

example

I = eye(sz) returns an array with ones on the main diagonal and zeros elsewhere. The size vector, sz, defines size(I). For example, eye([2,3]) returns a 2-by-3 array with ones on the main diagonal and zeros elsewhere.

example

I = eye(___,typename) also specifies the data type (class) of I for any of the previous syntaxes. For example, eye(5,'int8') returns a 5-by-5 identity matrix consisting of 8-bit integers.

example

I = eye(___,'like',p) specifies that I has the same data type, sparsity, and complexity (real or complex) as the numeric variable p.

## Examples

collapse all

Create a 4-by-4 identity matrix.

I = eye(4)
I = 4×4

1     0     0     0
0     1     0     0
0     0     1     0
0     0     0     1

Create a 2-by-3 identity matrix.

I = eye(2,3)
I = 2×3

1     0     0
0     1     0

Create a 3-by-1 identity vector.

sz = [3,1];
I = eye(sz)
I = 3×1

1
0
0

Create a 3-by-3 identity matrix whose elements are 32-bit unsigned integers.

I = eye(3,'uint32'),
I = 3x3 uint32 matrix

1   0   0
0   1   0
0   0   1

class(I)
ans =
'uint32'

Create a 2-by-2 identity matrix that is not real valued, but instead is complex like an existing array.

Define a complex vector.

p = [1+2i 3i];

Create an identity matrix that is complex like p.

I = eye(2,'like',p)
I = 2×2 complex

1.0000 + 0.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i   1.0000 + 0.0000i

Define a 5-by-5 sparse matrix.

p = sparse(5,5,pi);

Create a 5-by-5 identity matrix that is sparse like P.

I = eye(5,'like',p)
I =
(1,1)        1
(2,2)        1
(3,3)        1
(4,4)        1
(5,5)        1

Define a 2-by-2 matrix of single precision.

p = single([1 3 ; 2 4]);

Create an identity matrix that is the same size and data type as P.

I = eye(size(p),'like',p),
I = 2x2 single matrix

1     0
0     1

class(I)
ans =
'single'

## Input Arguments

collapse all

Size of first dimension of I, specified as an integer value.

• If n is the only integer input argument, then I is a square n-by-n identity matrix.

• If n is 0, then I is an empty matrix.

• If n is negative, then it is treated as 0.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of second dimension of I, specified as an integer value.

• If m is 0, then I is an empty matrix.

• If m is negative, then it is treated as 0.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of I, specified as a row vector of no more than two integer values.

• If an element of sz is 0, then I is an empty matrix.

• If an element of sz is negative, then the element is treated as 0.

Example: sz = [2 3] defines I as a 2-by-3 matrix.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output class, specified as 'double', 'single', logical, 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', or 'uint64'.

Prototype, specified as a numeric variable.

Data Types: double | single | logical | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

## Version History

Introduced before R2006a