How to Create the Identity Matrix in R?
Last Updated :
28 Nov, 2021
In this article, we will discuss how to create an Identity Matrix in R Programming Language.
Identity matrices are the matrices that contain all the zeros, except diagonal elements which are equivalent to 1. The identity matrices are always square in nature. Base R provides a large number of methods to create and define the identity matrices in R :
Method 1: Using diag method
The diag() method in base R is used to create a square matrix with the specified dimensions. It assigns the diagonal value to 1 and rest all the elements are assigned a value of 0.
Syntax:
diag(num)
where, num - The number equivalent to the number of rows and columns of the matrix.
Example:
R
# creating a diagonal matrix with
# dimensions 3 x 3
diag_mat < - diag(3)
# printing identity matrix
print("Identity Matrix")
print(diag_mat)
Output:
[1] "Identity Matrix"
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
Method 2: Using diag(nrow) method
The diag(nrow) method can be used to specify the number of rows of the identity matrix. It assigns the number of columns equivalent to the specified number of rows.
Syntax:
diag(nrow = )
where, nrow - The number of rows of the identity matrix
Example:
R
# creating a diagonal matrix with
# dimensions 7 x 7
diag_mat < -diag(nrow=7)
# printing identity matrix
print("Identity Matrix")
print(diag_mat)
Output:
[1] "Identity Matrix"
> print(diag_mat)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0
[3,] 0 0 1 0 0 0 0
[4,] 0 0 0 1 0 0 0
[5,] 0 0 0 0 1 0 0
[6,] 0 0 0 0 0 1 0
[7,] 0 0 0 0 0 0 1
Method 3: Creating a matrix of zeros and then assigning diagonals to 1
The matrix() method in R can be used to create a matrix with a specified value and assign it to the number of declared rows and columns of the matrix.
Syntax:
matrix ( val , rows, cols)
Parameters :
- val - The value to be assigned to all the cells
- rows - The rows of the identity matrix
- cols - The columns of the identity matrix
Example:
We initially create a matrix of 0's and then the diagonals are assigned 1's using the diag() method defined earlier.
R
# defining number of rows and columns
row < - 6
col < - 6
# creating a diagonal matrix with
# dimensions 6 x 6
diag_mat < - matrix(0, row, col)
# specifying the diagonal value to be 1
diag(diag_mat) < - 1
# printing identity matrix
print("Identity Matrix")
print(diag_mat)
Output:
[1] "Identity Matrix"
> print(diag_mat)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 0
[2,] 0 1 0 0 0 0
[3,] 0 0 1 0 0 0
[4,] 0 0 0 1 0 0
[5,] 0 0 0 0 1 0
[6,] 0 0 0 0 0 1
Similar Reads
How to create an empty matrix in R ?
The term empty matrix has no rows and no columns. A matrix that contains missing values has at least one row and column, as does a matrix that contains zeros. In this article, we are going to see how to create an empty matrix in R Programming Language. There are three ways of creating an empty matri
2 min read
How to create R Multiplication Table
In this article, we will look into how to write an R program if we are given an input 'n' and we need to print its multiplication table. Creating a multiplication table in R is a fundamental and helpful activity, particularly when you need to see how numbers multiply with one another or for instruct
6 min read
Convert Matrix to Vector in R
In this article, we are going to convert the given matrix into the vector in R programming language. Conversion of the matrix to vector by rowMethod 1: Using c() function Simply passing the name of the matrix will do the job. Syntax: c(matrix_name) Where matrix_name is the name of the input matrix E
3 min read
How to convert matrix to list of vectors in R ?
In this article, we will learn how to convert a matrix into a list of vectors in R Programming Language. Converting matrix into a list of vectors by Columns Method 1: Using as.list() function To convert columns of the matrix into a list of vectors, we first need to convert the matrix to a dataframe
4 min read
Convert matrix to list in R
In this article, we will discuss how to convert a given matrix to a List in R Programming Language. Conversion of a matrix into a list in Column-major order The as.list() is an inbuilt function that takes an R language object as an argument and converts the object into a list. We have used this func
2 min read
Convert Character Matrix to Numeric Matrix in R
In this article, we are going to see how to convert a given character matrix to numeric in R Programming Language. Converting the Character Matrix to Numeric Matrix we will use as.numeric() & matrix() Functions. Functions Usedas.numeric() function: This function is used to convert a given column
3 min read
Convert Matrix to Dataframe in R
In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the
1 min read
Sort Matrix According to First Column in R
In this article, we will discuss how to sort a matrix according to the first column in R programming language. Matrix in use: We can sort the matrix according to a specific column by using the order() function with indexing. To sort according to first column 1 is passed in place of column index. Syn
1 min read
How to Print a Vector in R
In this article, we'll look at how to print a vector's elements in R using a for loop. We'll go through a for loop's fundamental syntax, describe how to access vector members inside the loop, and give instances of several situations in which this method might be helpful. Learning how to use for loop
3 min read
Find the power of a matrix in R
In this article, we are going to see how to compute the power of a matrix in R Programming Language. Matrix is an arrangement of numbers into rows and columns. Different ways of finding the power of matrix in R programming: By using %^%.By using a power function. Method 1: By using %^% Before using
2 min read