identity matrix in R: How to create the identity matrix in R?

identity matrix in r

In this article, we will accompany you to learn how to create the identity matrix in R. There are some different ways that can help you create the identity matrix in R. Let’s learn more about how to do it with the explanation and examples below.

Diag in R

Before you learn how to create the identity matrix in R. Follow us to learn the usage and the syntax of the diag() function first.

What does diag do in R?

The diag() function in R is used to create or convert the values on the diagonal of the matrix. Let’s take a look at the syntax of this function.

Syntax

# To create the identity matrix.

diag(x = 1, nrow, ncol, names = TRUE)

# To assign value to the matrix diagonals

diag(x) <- value

Parameters:

  • x: Optional. The matrix or the vector.
  • nrow: The number of rows of the matrix.
  • ncol: The number of columns of the matrix.
  • names: The default is TRUE. Inherit the names from the dimnames(x) function if available or not.
  • value: For the second case, the value is assigned to the diagonal of the matrix.

How to create the identity matrix in R?

To create the identity matrix, you can use the diag() function which is built-in in the R language. Another solution, you can create your identity matrix by yourself without using the function in R. 

Use the diag() function 

You can use the diag() function to create the identity matrix. 

Look at the example below.

# Create the 4x4 identity square matrix
diag(4)

Output

      [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

Create the identity matrix with the specified row and column

You can create the identity matrix with the specified row and column by assigning the value to the ‘nrow’ parameter and ‘ncol’ parameter.

Look at the example below.

# Create the 4x5 identity square matrix.
diag(nrow = 4, ncol = 5)

Output

      [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    0    0
[2,]    0    1    0    0    0
[3,]    0    0    1    0    0
[4,]    0    0    0    1    0

Create the identity matrix with the specified value

You can create the identity matrix with the specified value by assigning a value to the diag() function.

In this example, you have to create the matrix first. You can learn how to create the matrix in R here.

Look at the example below.

# Create the 4x4 square matrix
mat <- matrix(0, 4, 4)

# Assign the value to the matrix diagonals as 3
diag(mat) <- 3
mat

Output

      [,1] [,2] [,3] [,4]
[1,]    3    0    0    0
[2,]    0    3    0    0
[3,]    0    0    3    0
[4,]    0    0    0    3

Summary

In this article, we have shown how to create the identity matrix in R by 3 different ways. The third solution: creating the identity matrix with the specified value is more customizable than two solutions before. But it is more complicated than them. Choosing the solution is the best for your case. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *