How to create an upper triangular matrix using vector elements in R?

R ProgrammingServer Side ProgrammingProgramming

To create an upper triangular matrix using vector elements, we can first create the matrix with appropriate number of columns and rows then take the transpose of that matrix. After that we will assign the lower triangular matrix elements to 0.

The selection of number of rows and columns plays an important role here so we need to be careful while choosing them.

Check out the examples given below to understand how it can be done.

Example 1

Following snippet creates a vector −

x1<-rpois(5,2)
x1

The following vector is created −

[1] 2 2 3 0 0

To create an upper triangular matrix using x1, add the following code to the above snippet −

x1<-rpois(5,2)
M1<-t(matrix(x1,nrow=9,ncol=5))[,1:5]
M1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

Add the following code to the above snippet −

M1[lower.tri(M1)]<-0
M1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

Example 2

Following snippet creates a vector −

x2<-rpois(10,5)
x2

Output

The following vector is created −

[1] 5 8 5 4 3 5 5 5 11 6

To create an upper triangular matrix using x2, add the following code to the above snippet −

x2<-rpois(10,5)
M2<-t(matrix(x2,nrow=19,ncol=10))[,1:10]
M2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   [,1][,2][,3][,4]  [,5] [,6] [,7] [,8] [,9] [,10]
[1,]  5  8   5    4    3   5   5     5   11    6
[2,]  6  5   8    5    4   3   5     5    5   11
[3,] 11  6   5    8    5   4   3     5    5    5
[4,]  5 11   6    5    8   5   4     3    5    5
[5,]  5  5  11    6    5   8   5     4    3    5
[6,]  5  5   5   11    6   5   8     5    4    3
[7,]  3  5   5    5   11   6   5     8    5    4
[8,]  4  3   5    5    5  11   6     5    8    5
[9,]  5  4   3    5    5   5  11     6    5    8
[10,] 8  5   4    3    5   5   5    11    6    5

Add the following code to the above snippet −

M2[lower.tri(M2)]<-0
M2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,]  5   8   5   4   3   5   5   5  11   6
[2,]  0   5   8   5   4   3   5   5   5  11
[3,]  0   0   5   8   5   4   3   5   5   5
[4,]  0   0   0   5   8   5   4   3   5   5
[5,]  0   0   0   0   5   8   5   4   3   5
[6,]  0   0   0   0   0   5   8   5   4   3
[7,]  0   0   0   0   0   0   5   8   5   4
[8,]  0   0   0   0   0   0   0   5   8   5
[9,]  0   0   0   0   0   0   0   0   5   8
[10,] 0   0   0   0   0   0   0   0   0   5

Example 3

Following snippet creates a vector −

x3<-rpois(3,28)
x3

The following vector is created −

[1] 21 33 24

To create an upper triangular matrix using x3, add the following code to the above snippet −

x3<-rpois(3,28)
M3<-t(matrix(x3,nrow=5,ncol=3))[,1:3]
M3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    [,1] [,2] [,3]
[1,] 21  33   24
[2,] 24  21   33
[3,] 33  24   21

Add the following code to the above snippet −

M3[lower.tri(M3)]<-0
M3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    [,1] [,2] [,3]
[1,] 21   33   24
[2,] 0    21   33
[3,] 0    0    21

Example 4

Following snippet creates a vector −

x4<-rpois(5,500)
x4

The following vector is created −

Output

[1] 495 499 478 502 531

To create an upper triangular matrix using x4, add the following code to the above snippet −

x4<-rpois(5,500)
M4<-t(matrix(x4,nrow=9,ncol=5))[,1:5]
M4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    [,1] [,2] [,3] [,4] [,5]
[1,] 495 499  478  502  531
[2,] 531 495  499  478  502
[3,] 502 531  495  499  478
[4,] 478 502  531  495  499
[5,] 499 478  502  531  495

Add the following code to the above snippet −

M4[lower.tri(M4)]<-0
M4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   [,1]  [,2] [,3] [,4] [,5]
[1,] 495 499  478  502  531
[2,] 0   495  499  478  502
[3,] 0    0   495  499  478
[4,] 0    0    0   495  499
[5,] 0    0    0    0   495

Example 5

Following snippet creates a vector −

x5<-rpois(10,100)
x5

The following vector is created −

[1] 109 85 108 86 76 99 113 75 82 86

To create an upper triangular matrix using x5, add the following code to the above snippet −

x5<-rpois(10,100)
M5<-t(matrix(x5,nrow=19,ncol=10))[,1:10]
M5

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    [,1] [,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,] 109  85 108  86  76  99 113  75  82   86
[2,]  86 109  85 108  86  76  99 113  75   82
[3,]  82  86 109  85 108  86  76  99 113   75
[4,]  75  82  86 109  85 108  86  76  99  113
[5,] 113  75  82  86 109  85 108  86  76   99
[6,]  99 113  75  82  86 109  85 108  86   76
[7,]  76  99 113  75  82  86 109  85 108   86
[8,]  86  76  99 113  75  82  86 109  85  108
[9,] 108  86  76  99 113  75  82  86 109   85
[10,] 85 108  86  76  99 113  75  82  86  109

Add the following code to the above snippet −

M5[lower.tri(M5)]<-0
M5

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,] 109  85 108  86  76  99 113  75  82   86
[2,]   0 109  85 108  86  76  99 113  75   82
[3,]   0   0 109  85 108  86  76  99 113   75
[4,]   0   0   0 109  85 108  86  76  99  113
[5,]   0   0   0   0 109  85 108  86  76   99
[6,]   0   0   0   0   0 109  85 108  86   76
[7,]   0   0   0   0   0   0 109  85 108   86
[8,]   0   0   0   0   0   0   0 109  85  108
[9,]   0   0   0   0   0   0   0   0 109   85
[10,]  0   0   0   0   0   0   0   0   0  109
raja
Published on 03-Nov-2021 08:02:11
Advertisements