How to set lower triangular matrices including main diagonal to zero stored in an R array?


The matrices stored in an array cannot directly have zero at lower triangular or upper triangular positions because assignment in arrays is not straight forward. Therefore, to set lower triangular matrices to zero stored in an R array, we can use for loop.

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

Example

Following snippet creates a sample array −

Array<-array(rnorm(5*5*4),dim=c(5,5,4))
Array

The following arrays are created −

, , 1

        [,1]          [,2]         [,3]       [,4]       [,5]
[1,]  0.70981523  -2.4078489    0.39454174  0.1926095  -1.0898185
[2,] -0.14475044   0.6219629  -0.95420653   0.4479287  -0.3779962
[3,]  0.01167506   2.1993791   0.07737409   0.6061328  -1.0125574
[4,]  0.79220776  -0.5975221   0.88549417   1.0882225  -0.5481808
[5,]  0.29491262  1.4220833    0.07935382  -1.0755292   0.7970269

, , 2

         [,1]         [,2]       [,3]        [,4]       [,5]
[1,]   0.8094188   0.4551336  -0.7160230   0.6137609   0.43214122
[2,]   0.5074892   1.4076603   1.1292408  -2.7777503   1.41570799
[3,]  -0.6848127  -1.2551047   1.4707569  -0.3999298   0.02225272
[4,]  -0.8430235  -1.0603518   1.0039837  -1.1010674  -0.73765708
[5,]   2.1778090  -0.2433661  -0.7438562  -0.4833542   1.45107435

, , 3

        [,1]       [,2]          [,3]        [,4]          [,5]
[1,]  1.3963751  0.552293381  0.9465197  0.45676297  0.814354214
[2,] -1.9652982 -0.007544944  1.0610379 -0.50206274 -1.408469903
[3,]  0.5089749  0.129974481 -1.6970820 -1.26033470  0.134300795
[4,] -0.7690704  0.796328881  0.2983638  0.09370386  0.092306831
[5,]  0.8453528  0.103282791 -0.7055156  0.35558281 -0.008647578

, , 4

          [,1]       [,2]       [,3]     [,4]       [,5]
[1,]  0.90802766 -0.4069328 -1.5392820  1.0841168 -0.1180422
[2,] -0.04871904  0.3874517  0.9413034 -0.5000461 -0.7223448
[3,]  0.85111729 -1.6321968 -1.1475875  0.5568671 -0.2763780
[4,]  0.67846366 -1.0864209 -1.1823581 -0.2222426 -0.4751920
[5,] -1.87861268  0.5262939  0.1984496 -0.4013330  0.2765006

Now, in order to set the upper triangular matrices in Array to zero including main diagonal, add the following code to the above snippet −

Array<-array(rnorm(5*5*4),dim=c(5,5,4))
for(i in seq(dim(Array)[3])) Array[,,i][lower.tri(Array[,,i],diag=TRUE)]<-0
Array

Output

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

, , 1

    [,1]  [,2]      [,3]        [,4]        [,5]
[1,] 0  -2.407849  0.3945417  0.1926095  -1.0898185
[2,] 0   0.000000 -0.9542065  0.4479287  -0.3779962
[3,] 0   0.000000  0.0000000  0.6061328  -1.0125574
[4,] 0   0.000000  0.0000000  0.0000000  -0.5481808
[5,] 0   0.000000  0.0000000  0.0000000   0.0000000

, , 2

    [,1]     [,2]       [,3]      [,4]      [,5]
[1,] 0   0.4551336  -0.716023   0.6137609   0.43214122
[2,] 0   0.0000000   1.129241  -2.7777503   1.41570799
[3,] 0   0.0000000   0.000000  -0.3999298   0.02225272
[4,] 0   0.0000000   0.000000   0.0000000  -0.73765708
[5,] 0   0.0000000   0.000000   0.0000000   0.00000000

, , 3

    [,1]    [,2]      [,3]        [,4]       [,5]
[1,] 0   0.5522934  0.9465197   0.4567630   0.81435421
[2,] 0   0.0000000  1.0610379  -0.5020627  -1.40846990
[3,] 0   0.0000000  0.0000000  -1.2603347   0.13430080
[4,] 0   0.0000000  0.0000000   0.0000000   0.09230683
[5,] 0   0.0000000  0.0000000   0.0000000   0.00000000

, , 4

    [,1]    [,2]         [,3]      [,4]       [,5]
[1,] 0  -0.4069328  -1.5392820  1.0841168  -0.1180422
[2,] 0   0.0000000   0.9413034 -0.5000461  -0.7223448
[3,] 0   0.0000000   0.0000000  0.5568671  -0.2763780
[4,] 0   0.0000000   0.0000000  0.0000000  -0.4751920
[5,] 0   0.0000000   0.0000000  0.0000000   0.0000000

Updated on: 05-Nov-2021

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements