Open In App

Convert a matrix into a lower triangular matrix using R

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

In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language.

What is a matrix?

A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are vertical. A matrix can contain data of the same type such as numeric, character, and, logic. It is possible to perform various operations on matrices. These matrixes are very similar to the vectors but differ in their dimensionalities. Using the function ‘matrix()’ can create the matrix.

Converting a matrix into various types of lower triangular matrices

R language offers different types of lower triangular matrices. In the lower triangular matrix, we perform operations on above the principal diagonal. when a matrix is converted into a lower triangular, the data that is present above the principal diagonal is zero in every entry. Some of the types of lower triangular matrices are:

  1. Lower triangular matrix
  2. Strictly lower triangular matrix
  3. Unit lower traingular matrix

Lower triangular matrix

A lower traingular matrix s a square matrix, whose elements are above the principal diagonal are zero. The syntax for lower traingular matrix is

upper.tri(matrix)

In this example, we created 3*3 matrix and converted it as lower traingular matrix.

R
#creating a matrix
mat=matrix(c(11:19),ncol=3)
print(mat)

print("The lower traingular matrix")
mat[upper.tri(mat)]=0
print(mat)

Output:

     [,1] [,2] [,3]
[1,]   11   14   17
[2,]   12   15   18
[3,]   13   16   19
[1] "The lower traingular matrix"
[,1] [,2] [,3] [1,] 11 0 0 [2,] 12 15 0 [3,] 13 16 19

In this example, we created 5*5 matrix and converted it as lower traingular matrix.

R
#vectoor
vec=c(1:25)
#creating a matrix
mat=matrix(c(1:25),nrow=5)
print(mat)

print("The lower traingular matrix")
mat[upper.tri(mat)]=0
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] "The lower traingular matrix"
[,1] [,2] [,3] [,4] [,5] [1,] 1 0 0 0 0 [2,] 2 7 0 0 0 [3,] 3 8 13 0 0 [4,] 4 9 14 19 0 [5,] 5 10 15 20 25

Strictly lower triangular matrix

Strictly lower triangular matrix is a square matrix where all the elements on or above the principal diagonal is zero. In this example, we created 5*5 matrix and converted it as strictly lower traingular matrix.

R
# Creating a matrix
mat <- matrix(1:25, nrow = 5)
print(mat)
# Creating a matrix
mat <- mat * lower.tri(mat)

print("strictly lower traingular matrix")
#printing
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] "strictly lower traingular matrix"
[,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 2 0 0 0 0 [3,] 3 8 0 0 0 [4,] 4 9 14 0 0 [5,] 5 10 15 20 0

In this example, we created 3*3 matrix and converted it as strictly lower traingular matrix.

R
# Create vector
vec=(1:9)
# Creating a matrix
mat <- matrix(vec, nrow = 3)
print(mat)
# Creating a matrix
mat <- mat * lower.tri(mat)

print("strictly lower traingular matrix")
print(mat)

Output:

    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[1] "strictly lower traingular matrix"
[,1] [,2] [,3] [1,] 0 0 0 [2,] 2 0 0 [3,] 3 6 0

Unit lower traingular matrix

A unit lower traingular matrix is a square matrix where all the diagonal elements are equal to 1 and above the diagonal are zeros. In this example, we created 5*5 matrix and converted it as unit lower traingular matrix.

R
# To generate unit lower triangular matrix
m1=function(n) {
  mat=matrix(0, nrow = n, ncol = n)  
  
  for (i in 1:n) {
    # Loop for each column up to the diagonal
    for (j in 1:i) {
      mat[i, j] <- 1  # Set diagonal to 1 
    }
  }
  return(mat)
}

n=5  
m1=m1(n)
print(m1)

Output:

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

In this example, we created 3*3 matrix and converted it as unit lower traingular matrix.

R
# To generate unit lower triangular matrix
m1=function(n) {
  mat=matrix(0, nrow = n, ncol = n)  
  
  for (i in 1:n) {
    # Loop for each column up to the diagonal
    for (j in 1:i) {
      mat[i, j] <- 1  # Set diagonal to 1 
    }
  }
  return(mat)
}

n=3
m1=m1(n)
print("unit lower traingular matrix")
print(m1)

Output:

[1] "unit lower traingular matrix"
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    1    1    0
[3,]    1    1    1

Conclusion

In conclusion, we learned about converting a matrix into various lower triangular matrices. R language offers versatile tools while handling with the matrices.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads

Article Tags :