**identity matrix**(also known as the

**eye matrix**in MATLAB) plays a vital role in matrix theory. It can be used to perform many matrix operations. It acts as the multiplicative identity in the matrix theory means when we multiply a matrix with its inverse, we obtain a matrix that is an

**identity matrix**. This matrix can be created in MATLAB using the

**eye()**function.

This article will explore:

- What is the Identity Matrix?
- How to Generate an Identity Matrix in MATLAB?
- Example 1: How to Implement eye Function in MATLAB?
- Example 2: How to Create a Square Identity Matrix in MATLAB?
- Example 3: How to Create Rectangular Identity Matrix in MATLAB?
- Example 4: How to Create a 2-D Identity Array in MATLAB?
- Example 5: How to Create An Identity Matrix Having Non-default Data Type in MATLAB?
- Example 6: How to Generate Sparse Identity Matrix in MATLAB?
- Example 7: How to Create a Complex Identity Matrix in MATLAB?
- Example 8: How to Create an Identity Matrix Defining Size and Data Type from the Existing Matrix in MATLAB?
- Conclusion

## What is the Identity Matrix?

An identity matrix is a matrix type that has the value “1” on its diagonal position and “0s” on the non-diagonal positions. A 3-by-3 identity matrix is given below:

## How to Generate an Identity Matrix in MATLAB?

We can easily and efficiently create an identity matrix in MATLAB using the built-in eye() function. This function accepts no argument to create a 1-by-1 identity matrix or it accepts the size of the identity matrix to create it.

**Syntax**

The eye() function can be used through the following ways in MATLAB.

I = eye(n)

I = eye(n,m)

I = eye(sz)

I = eye(___,”typename”)

I = eye(___,'like',p)

Here.

The function **I = eye** is responsible for creating a one-by-one identity matrix containing only one element that is “1”.

The function **I = eye(n)** is responsible for creating a square n-by-n identity matrix having “1” on the diagonal position and “0s” on the non-diagonal positions.

The function **I = eye(n,m)** is responsible for creating a rectangular n-by-m identity matrix having “1” on the main diagonal position and “0s” on the non-diagonal positions.

The function **I = eye(sz)** is responsible for creating an identity array of the size sz containing “1” on the diagonal position and “0s” on the non-diagonal positions. Here, sz defines a vector that contains the size of the identity array. For example, if sz=[2,4], the size of the created identity array will be 2-by-4. Keep in mind that the eye() function does not support the N-dimensional arrays.

The function **I = eye(___,typename)** is responsible for creating an identity array using any of the previous syntaxes by specifying the data type or class of I. For example, eye(3,”int16”) will create a 3-by-3 identity matrix of the data type “int16”.

The function **I = eye(___,’like’,p)** is responsible for creating an identity array using any of the previous syntaxes having the same sparsity, data type, and complexity as the given numeric variable p.

## Example 1: How to Implement eye Function in MATLAB?

The given example creates a one-by-one **identity matrix** using the **eye **function in MATLAB.

## Example 2: How to Create a Square Identity Matrix in MATLAB?

In this MATLAB code, we create a square 9-by-9 **identity matrix** in MATLAB using the **eye(n)** function for **n=9.**

## Example 3: How to Create Rectangular Identity Matrix in MATLAB?

In this example, we create a rectangular 7-by-9 **identity matrix** using the **eye(n,m)** function for **n=7** and **m=9**.

## Example 4: How to Create a 2-D Identity Array in MATLAB?

This MATLAB code uses the **eye(sz)** function to create a **two-dimensional identity array** in MATLAB by defining the size of the array in the vector **sz**.

A = eye(sz)

## Example 5: How to Create An Identity Matrix Having Non-default Data Type in MATLAB?

This example uses the **eye(___,”typename”)** function to create a square **3-by-3 identity matrix** having an **unsigned int 32** data type.

## Example 6: How to Generate Sparse Identity Matrix in MATLAB?

In this example, first, we initialize a **rectangular 3-by-7 sparse matrix p**. After that we use the **eye(___,’like’,p)** function to create a **4-by-4 identity matrix** that behaves like the above-created **sparse matrix p.**

A = eye(4,'like',p)

## Example 7: How to Create a Complex Identity Matrix in MATLAB?

The below-given MATLAB code first creates a **1-by-3 complex vector p**. After that it uses the **eye(___,’like’,p)** function to create a **3-by-3 complex identity matrix** like the above-created complex vector **p**.

A = eye(3,'like',p)

## Example 8: How to Create an Identity Matrix Defining Size and Data Type from the Existing Matrix in MATLAB?

The given MATLAB code first creates a **2-by-2 matrix **using the **magic()** function and then uses the function **eye(___,’like’,p)** to create an **identity matrix** having the **size **and **data type** same as the above-created matrix **p**.

A = eye(size(p),'like',p)

## Conclusion

An **identity matrix** is a type of matrix that contains the value “1” on its diagonal position and “0s” on its non-diagonal positions. These matrices can be easily created in MATLAB using the eye() function. This guide has presented all possible methods and examples for creating an identity matrix in MATLAB.