How to find the mean of a square matrix elements by excluding diagonal elements in R?


There are many ways to find the mean of a matrix elements by excluding diagonal elements, this mean is actually the mean of lower triangular matrix and the upper triangular matrix. We can simply use mean function by creating a vector of lower and upper triangular matrix as shown in the below examples.

Example1

Live Demo

> M1<-matrix(rpois(20,5),nrow=5)
> M1

Output

   [,1] [,2] [,3] [,4]
[1,] 1    6    3    6
[2,] 8    5    3    4
[3,] 5    4    4    6
[4,] 5    5    3    4
[5,] 4    8    1    3

Example

> mean(c(M1[upper.tri(M1)],M1[lower.tri(M1)]))

Output

[1] 4.625

Example2

Live Demo

> M2<-matrix(rpois(100,5),ncol=10)
> M2

Output

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

Example

> mean(c(M2[upper.tri(M2)],M2[lower.tri(M2)]))

Output

[1] 4.844444

Example3

Live Demo

> M3<-matrix(rnorm(36,6,2.1),nrow=6)
> M3

Output

        [,1]      [,2]     [,3]     [,4]    [,5]    [,6]
[1,] 11.102747 3.667257 2.674908 1.685490 7.297186 3.675492
[2,] 5.514661 7.012920 5.452867 5.890710 3.347643 4.944706
[3,] 5.842299 5.090597 5.764840 5.274849 7.210516 6.025230
[4,] 4.848784 6.171407 5.374152 10.452946 5.642747 8.886984
[5,] 9.387442 9.055521 8.677278 5.539050 5.648067 6.655354
[6,] 8.664883 5.976989 3.664106 5.527728 6.268774 8.135999

Example

> mean(c(M3[upper.tri(M3)],M3[lower.tri(M3)]))

Output

[1] 5.797854

Example4

Live Demo

> M4<-matrix(sample(11:20,64,replace=TRUE),nrow=8)
> M4

Output

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

Example

> mean(c(M4[upper.tri(M4)],M4[lower.tri(M4)]))

Output

[1] 15.91071

Example5

Live Demo

> M5<-matrix(runif(36,2,10),ncol=6)
> M5

Output

       [,1]     [,2]      [,3]     [,4]     [,5]    [,6]
[1,] 5.315719 7.590613 2.964828 2.510046 8.887437 3.377134
[2,] 3.393052 3.308531 5.938284 9.757756 7.803694 7.147399
[3,] 7.140688 7.942123 9.287733 6.907886 4.403413 2.938388
[4,] 3.171102 6.164160 4.408718 6.946996 5.626955 9.479825
[5,] 6.982951 8.210883 3.677973 9.643256 6.250810 6.695614
[6,] 7.575222 9.967751 6.816987 3.348135 7.717623 6.192853

Example

> mean(c(M5[upper.tri(M5)],M5[lower.tri(M5)]))

Output

[1] 6.272997

Example6

Live Demo

> M6<-matrix(sample(501:999,64),nrow=8)
> M6

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 990 725 549 896 867 619 764 682
[2,] 602 593 579 829 538 964 939 864
[3,] 889 878 851 911 886 834 693 730
[4,] 736 969 603 545 849 936 515 532
[5,] 982 856 771 925 877 972 647 570
[6,] 890 655 961 883 504 843 950 537
[7,] 766 726 628 870 899 585 687 536
[8,] 853 633 560 729 887 680 527 629

Example

> mean(c(M6[upper.tri(M6)],M6[lower.tri(M6)]))

Output

[1] 756.4286

Updated on: 23-Nov-2020

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements