Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

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 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

collapse all

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

Create a 2-by-3 identity matrix.

I = eye(2,3)
I =

     1     0     0
     0     1     0

Create a 3-by-1 identity vector.

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

     1
     0
     0

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

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

  3×3 uint32 matrix

   1   0   0
   0   1   0
   0   0   1


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 =

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

  2×2 single matrix

     1     0
     0     1


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'.

Data Types: char

Prototype, specified as a numeric variable.

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

See Also

| |

Introduced before R2006a


Was this topic helpful?