How to find the sum of anti-diagonal elements in a matrix in R?


The anti-diagonal elements in a matrix are the elements that form straight line from right upper side to right bottom side. For example, if we have a matrix as shown below −

1 2 3
4 5 6
7 8 9

then the diagonal elements would be 1, 5, 9 and the anti-diagonal elements would be 3, 5, 7.

To find the sum of these anti-diagonal elements, we can use apply function.

Example

 Live Demo

M1<-matrix(1:9,ncol=3)
M1

Output

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

Example

sum(diag(apply(M1,2,rev)))

Output

[1] 15

Example

 Live Demo

M2<-matrix(1:100,nrow=10)
M2

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 
[1,] 1   11   21   31   41   51   61   71   81   91 
[2,] 2   12   22   32   42   52   62   72   82   92 
[3,] 3   13   23   33   43   53   63   73   83   93 
[4,] 4   14   24   34   44   54   64   74   84   94 
[5,] 5   15   25   35   45   55   65   75   85   95 
[6,] 6   16   26   36   46   56   66   76   86   96 
[7,] 7   17   27   37   47   57   67   77   87   97 
[8,] 8   18   28   38   48   58   68   78   88   98 
[9,] 9   19   29   39   49   59   69   79   89   99 
[10,] 10  20  30   40   50   60   70   80   90  100

sum(diag(apply(M2,2,rev))) [1] 505

Example

 Live Demo

M3<-matrix(sample(0:9,36,replace=TRUE),nrow=6)
M3

Output

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

sum(diag(apply(M3,2,rev))) [1] 34

Example

 Live Demo

M4<-matrix(sample(1:10,64,replace=TRUE),nrow=8)
M4

Output

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

sum(diag(apply(M4,2,rev))) [1] 54

Example

 Live Demo

M5<-matrix(sample(1:100,81),nrow=9)
M5

Output

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 
[1,]  32   34   99   73   93   65   82   50   9 
[2,]  49   69   62   37   96   40  57  97    86 
[3,]  11   84   22   53   87   12   95   88 100
 [4,]  44 77   48   58   71   78   2   10    45
 [5,]  39 66  72   23   24   20   55   59   35 
[6,]  18   79  52  98   29   43   7    75   74
 [7,]  80  15  70   91   13  60   61   1   38 
[8,]  41   5   4   17   46   30   26   81   21
 [9,]  54  51  6   25   47   89   36   85   67

sum(diag(apply(M5,2,rev))) [1] 530

Example

 Live Demo

M6<-matrix(sample(101:999,36),nrow=6)
M6

Output

   [,1] [,2] [,3] [,4] [,5] [,6] 
[1,] 726  139 975 492  672 686 
[2,] 501  754 818 724  547 446 
[3,] 204  480 530 112  872 761
[4,] 789  165 572 899  538 298 
[5,] 987  119 274 369  936 132 
[6,] 306  696 448 618  951 137

sum(diag(apply(M6,2,rev))) [1] 2342

Example

 Live Demo

M7<-matrix(rpois(49,5),nrow=7)
M7

Output

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

sum(diag(apply(M7,2,rev))) [1] 35

Example

 Live Demo

M8<-matrix(rpois(81,3),nrow=9)
M8

Output

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

sum(diag(apply(M8,2,rev))) [1] 24

Updated on: 18-Oct-2020

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements