Open In App

Replace the Diagonal of a Matrix using R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn what a is matrix and various methods to replace the diagonal of a matrix in the R Programming Language.

What is a matrix?

A matrix is a two-dimensional data set, a collection of rows and columns. Inside the matrix, rows are arranged horizontally, and columns are arranged vertically. Matrices could contain data of many types such as strings, integers, characters, and logic. By using the function ‘matrix()’ matrices are created.

How do we replace the diagonal of a matrix?

R language provides various ways to replace the diagonal of a matrix efficiently. It’s crucial to comprehend that diagonal elements are relevant only in a square matrix. Attempting to replace diagonal elements in non-square matrices is nonsensical and might lead to confusion, especially for those new to matrix operations. some of the ways to replace the diagonal of a matrix are:

  1. Using simple indexing
  2. Using matrix subsetting
  3. Using for loop

Replace the diagonal of a matrix using simple indexing

This method can access the diagonal elements by using the function ‘diag()’ and replaces with specific values. The syntax to replace the diagonal of a matrix is.

diag(matrix)

In this example, we created 4×4 matrix and replaced the diagonal elements with a specified values.

R
#creating a matrix
m1=matrix(c(1:9),ncol=3)
print(m1)                              

a=c(5,10,15)
print("After replacing the diagonal ofthe matrix is")
diag(m1)=a
print(m1)                             

Output:

     [,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] "After replacing the diagonal ofthe matrix is"
[,1] [,2] [,3]
[1,] 5 4 7
[2,] 2 10 8
[3,] 3 6 15

In this example, we created 5×5 matrix and replaced the diagonal elements with a specified values.

R
#vector
c=c(1:25)

#creating a matrix
m1=matrix(c,ncol=5)
print(m1)                             

a=c(10,20,30,40,50)
print("After replacing the diagonal of a matrix")
diag(m1)=a
print(m1)                               

Output:

     [,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[1] "After replacing the diagonal of a matrix"

[,1] [,2] [,3] [,4] [,5]
[1,] 10 6 11 16 21
[2,] 2 20 12 17 22
[3,] 3 8 30 18 23
[4,] 4 9 14 40 24
[5,] 5 10 15 20 50

Replace the diagonal of a matrix Using matrix subsetting

Subsetting is the other way to replace the diagonal of the matrix. The syntax for matrix subsetting is:

subset matrix=matrix[row indices, column indices]

In this example, we created 5×5 matrix and replaced the diagonal elements with a specified values.

R
# Creating a matrix
mat=matrix(1:25, nrow = 5)
b=c(10,20,30,40,50)
print(mat)

# Replace diagonal elements with a specific value
print("After replacing the diagonal is ")
diag=cbind(1:nrow(mat), 1:ncol(mat))
mat=replace(mat, diag, b)
print(mat)

Output:

     [,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[1] "After replacing the diagonal is "
[,1] [,2] [,3] [,4] [,5]
[1,] 10 6 11 16 21
[2,] 2 20 12 17 22
[3,] 3 8 30 18 23
[4,] 4 9 14 40 24
[5,] 5 10 15 20 50

In this example, we created 3×3 matrix and replaced the diagonal elements with a specified values.

R
# Original vector
a = c(1, 2, 3, 4, 5, 6, 7, 8, 9)

# Creating a matrix
mat = matrix(a, nrow = 3)
print("Original Matrix:")
print(mat)

# Replacement vector
b = c(5, 10, 15)

# Getting the diagonal indices
diag_indices = cbind(1:nrow(mat), 1:ncol(mat))

# Replacing the diagonal elements with values from vector b
mat[diag_indices] = b

print("After replacing the diagonal:")
print(mat)

Output:

[1] "Original Matrix:"
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] "After replacing the diagonal:"
[,1] [,2] [,3]
[1,] 5 4 7
[2,] 2 10 8
[3,] 3 6 15

Replace the diagonal of a matrix using for loop

This method iterates the diagonal elements using for loop and replaces it with specific values.The syntax is :


for(variable in sequence)
{
#block of code
}

In this example, we created 3×3 matrix and replaced the diagonal elements with a specified values.

R
# Creating a matrix
m1 <- matrix(1:9, nrow = 3)

print(m1)
a=c(10, 11, 12)

for (i in 1:min(nrow(m1), ncol(m1))) {
  m1[i, i]=a[i]
}

print("After replacing the diagonal ")
# Displaying the matrix
print(m1)

Output:

     [,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] "After replacing the diagonal "
[,1] [,2] [,3]
[1,] 10 4 7
[2,] 2 11 8
[3,] 3 6 12

In this example, we created 5×5 matrix and replaced the diagonal elements with a specified values.

R
vec=c(1:25)
# Creating a matrix
m1 <- matrix(vec, nrow = 5)

print(m1)
a=c(40,45,55,60,65)

for (i in 1:min(nrow(m1), ncol(m1))) {
  m1[i, i]=a[i]
}

print("After replacing the diagonal")
# Displaying the matrix
print(m1)

Output:

     [,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[1] "After replacing the diagonal"
[,1] [,2] [,3] [,4] [,5]
[1,] 40 6 11 16 21
[2,] 2 45 12 17 22
[3,] 3 8 55 18 23
[4,] 4 9 14 60 24
[5,] 5 10 15 20 65

Conclusion

In conclusion, we learned about how to replace the diagonal of a matrix by using various methods. R language provides versatile tools while handling with matrices.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads