eye

Identity matrix

Syntax

  • I = eye(classname)
  • I = eye(n,classname) example
  • I = eye(n,m,classname)
  • I = eye(sz,classname)
  • I = eye('like',p)
  • I = eye(n,'like',p) example
  • I = eye(n,m,'like',p)
  • I = eye(sz,'like',p) example

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.

I = eye(classname) returns a scalar, 1, where the string, classname, specifies the data type. For example, eye('int8') returns a scalar, 8-bit integer.

example

I = eye(n,classname) returns an n-by-n identity matrix of data type classname.

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

I = eye(sz,classname) returns a matrix with ones on the main diagonal and zeros elsewhere. The size vector, sz, defines size(I) and classname defines class(I).

I = eye('like',p) returns a scalar, 1, with the same data type, sparsity, and complexity (real or complex) as the numeric variable, p.

example

I = eye(n,'like',p) returns an n-by-n identity matrix like p.

I = eye(n,m,'like',p) returns an n-by-m matrix like p.

example

I = eye(sz,'like',p) returns a matrix like p where the size vector, sz, defines size(I).

Examples

expand all

Square Identity Matrix

Create a 4-by-4 identity matrix.

I = eye(4)
I =

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

Rectangular Matrix

Create a 2-by-3 identity matrix.

I = eye(2,3)
I =

     1     0     0
     0     1     0

Identity Vector

Create a 3-by-1 identity vector.

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

     1
     0
     0

Nondefault Numeric Data Type

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

I = eye(3,'uint32'),
class(I)
I =

           1           0           0
           0           1           0
           0           0           1


ans =

uint32

Complex Identity Matrix

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 =

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

Sparse Identity Matrix

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

Size and Numeric Data Type Defined by Existing Array

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),
class(I)
I =

     1     0
     0     1


ans =

single

Input Arguments

expand all

n — Size of first dimension of Iinteger value

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

m — Size of second dimension of Iinteger value

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

sz — Size of Irow vector of no more than two integer values

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

classname — Output class'double' (default) | 'single' | 'int8' | 'uint8' | ...

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

Data Types: char

p — Prototypenumeric variable

Prototype, specified as a numeric variable.

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

See Also

| |

Was this topic helpful?