How to replace upper triangular matrix with lower triangular matrix and vice versa in R?


The upper triangular matrix can be replaced with lower triangular matrix by transposing the whole matrix and extracting upper triangular matrix from it then storing it in the original matrix. For example, if we have a matrix M then upper triangular matrix of M can be replaced with lower triangular matrix by using the below code −

M1[upper.tri(M1)]<-t(M1)[upper.tri(M1)]

It can be also done for lower triangular matrix in the similar manner.

Example

 Live Demo

M1<-matrix(1:25,ncol=5)
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

Example

M1[upper.tri(M1)]<-t(M1)[upper.tri(M1)]
M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1    2    3    4    5
[2,] 2    7    8    9    10
[3,] 3    8    13   14   15
[4,] 4    9    14   19   20
[5,] 5    10   15   20   25

Example

 Live Demo

M2<-matrix(rpois(100,10),nrow=10)
M2

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3    12   16   6    4    11    12 12    9    14
[2,] 12   9    9    11   10    14   13 10    10   13
[3,] 17   14   8    12   12    15   9   8    15   4
[4,] 17   6    9    17   11    8    11 15    14   12
[5,] 11   14   7    7    12    9    9   8    14   13
[6,] 13   7    11   8    10    3    13 11    6    11
[7,] 13   11   7    11   15    9    12 10    11   7
[8,] 13   10   3    8    7    11    13 9      7   11
[9,] 7    18   15   11   12   14    10 5      7   15
[10,] 13   7   15   11   11    9    12 6      11   10

Example

M2[upper.tri(M2)]<-t(M2)[upper.tri(M2)]
M2

Output

   [,1] [,2] [,3]   [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3   12    17    17   11   13   13    13   7    13
[2,] 12   9    14    6    14   7    11    10   18    7
[3,] 17   14    8    9    7    11    7    3    15    15
[4,] 17   6     9    17   7    8    11    8    11    11
[5,] 11   14    7    7    12   10   15    7    12    11
[6,] 13   7    11    8    10   3    9     11   14    9
[7,] 13   11    7    11   15   9    12    13   10    12
[8,] 13   10    3    8    7    11   13    9    5      6
[9,] 7    18    15   11   12   14   10    5    7      11
[10,] 13   7    15   11   11   9    12    6    11    10

Example

 Live Demo

M3<-matrix(rpois(64,5),nrow=8)
M3

Output

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

Example

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

Output

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

Example

 Live Demo

M4<-matrix(rnorm(36),nrow=6)
M4

Output

         [,1]       [,2]       [,3]       [,4]       [,5]    [,6]
[1,] 0.05023214 -0.3817478 0.7079296 0.8578374 -1.5239053 0.1864117
[2,] -0.73729460 1.5129931 0.2135871 0.9721983 -0.2416306 -0.5309397
[3,] -0.24488229 -0.6229905 0.8579926 1.3248632 -1.9305997 0.5397310
[4,] -1.24118039 -1.1080773 -0.9903481 0.1033527 0.4685492 0.1691699
[5,] 1.60606286 -0.9149534 -1.3265918 2.0232826 0.5383570 0.9284483
[6,] 1.29482725 -0.7275765 1.1407260 -0.8860395 -0.3185204 -1.0165007

Example

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

Output

      [,1]          [,2]       [,3]      [,4]       [,5]    [,6]
[1,] 0.05023214 -0.3817478 0.7079296 0.8578374 -1.5239053 0.1864117
[2,] -0.38174778 1.5129931 0.2135871 0.9721983 -0.2416306 -0.5309397
[3,] 0.70792961 0.2135871 0.8579926 1.3248632 -1.9305997 0.5397310
[4,] 0.85783742 0.9721983 1.3248632 0.1033527 0.4685492 0.1691699
[5,] -1.52390534 -0.2416306 -1.9305997 0.4685492 0.5383570 0.9284483
[6,] 0.18641175 -0.5309397 0.5397310 0.1691699 0.9284483 -1.0165007

Example

 Live Demo

M5<-matrix(rexp(9),nrow=3)
M5

Output

         [,1]       [,2]    [,3]
[1,] 0.8228933 0.05741123 0.3246455
[2,] 0.1750773 0.08412468 2.1097830
[3,] 0.3997029 1.18019289 0.1099468

Example

M5[upper.tri(M5)]<-t(M5)[upper.tri(M5)]
M5

Output

         [,1]       [,2]    [,3]
[1,] 0.8228933 0.17507725 0.3997029
[2,] 0.1750773 0.08412468 1.1801929
[3,] 0.3997029 1.18019289 0.1099468

Updated on: 07-Dec-2020

590 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements