# How to extract diagonal elements of a matrix in R without using diag function?

R ProgrammingServer Side ProgrammingProgramming

The diagonal elements of a matrix occur at the position where column and row indices are same hence, we can make use of those indices to extract diagonal elements of a matrix if we do not want to use diag function.

For example, if we have a matrix called M then diagonal elements of M can be extracted by using the command given below −

M[row(M)==col(M)]

Check out the below examples to understand how it works.

## Example 1

Following snippet creates a matrix −

M1<-matrix(rpois(25,1),ncol=5)
M1

The following matrix is created −

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

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M1<-matrix(rpois(25,1),ncol=5)
M1[row(M1)==col(M1)]

## Output

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

[1] 0 0 0 1 1


## Example 2

Following snippet creates a matrix −

M2<-matrix(rpois(25,2),ncol=5)
M2

The following matrix is created −

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

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M2<-matrix(rpois(25,2),ncol=5)
M2[row(M2)==col(M2)]

## Output

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

[1] 2 4 1 2 0


## Example 3

Following snippet creates a matrix −

M3<-matrix(rpois(25,10),ncol=5)
M3

The following matrix is created −

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

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M3<-matrix(rpois(25,10),ncol=5)
M3[row(M3)==col(M3)]

## Output

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

[1] 7 9 15 11 7


## Example 4

Following snippet creates a matrix −

M4<-matrix(rpois(25,50),ncol=5)
M4

The following matrix is created −

   [,1][,2][,3][,4][,5]
[1,] 48 40  42  44  53
[2,] 46 47  56  43  41
[3,] 47 54  53  40  50
[4,] 39 50  53  55  48
[5,] 57 57  43  57  64

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M4<-matrix(rpois(25,50),ncol=5)
M4[row(M4)==col(M4)]

## Output

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

[1] 48 47 53 55 64


## Example 5

Following snippet creates a matrix −

M5<-matrix(rpois(25,500),ncol=5)
M5

The following matrix is created −

    [,1][,2][,3][,4][,5]
[1,] 501 506 543 506 518
[2,] 466 531 530 496 505
[3,] 510 497 516 522 505
[4,] 455 521 504 476 493
[5,] 465 520 490 501 476

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M5<-matrix(rpois(25,500),ncol=5)
M5[row(M5)==col(M5)]

## Output

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

[1] 501 531 516 476 476


## Example 6

Following snippet creates a matrix −

M6<-matrix(round(rnorm(25),1),ncol=5)
M6

The following matrix is created −

    [,1]  [,2] [,3] [,4] [,5]
[1,] -2.1  1.0  0.3  1.4 -1.2
[2,] -0.1  0.1 -0.2 -1.1  0.7
[3,] -1.6 -0.6 -0.2  1.5 -1.0
[4,] -0.7  1.2 -1.5 -0.9  0.1
[5,]  1.2  0.5  1.8 -0.6  0.4

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M6<-matrix(round(rnorm(25),1),ncol=5)
M6[row(M6)==col(M6)]

## Output

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

[1] -2.1 0.1 -0.2 -0.9 0.4


## Example 7

Following snippet creates a matrix −

M7<-matrix(round(runif(25,2,5),2),ncol=5)
M7

The following matrix is created −

     [,1] [,2] [,3] [,4] [,5]
[1,] 4.63 3.45 2.22 4.31 3.46
[2,] 2.22 3.65 3.15 4.19 2.65
[3,] 3.14 3.68 2.31 3.04 3.45
[4,] 3.71 4.91 3.94 2.55 2.98
[5,] 3.48 2.54 2.29 3.58 3.15

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M7<-matrix(round(runif(25,2,5),2),ncol=5)
M7[row(M7)==col(M7)]

## Output

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

[1] 4.63 3.65 2.31 2.55 3.15


## Example 8

Following snippet creates a matrix −

M8<-matrix(round(rexp(25,2.05),2),ncol=5)
M8

The following matrix is created −

     [,1] [,2] [,3] [,4] [,5]
[1,] 0.60 0.15 0.32 0.42 0.16
[2,] 0.59 0.40 0.95 0.23 1.07
[3,] 0.02 0.57 0.79 0.21 0.09
[4,] 1.03 0.25 0.25 1.08 0.46
[5,] 0.10 0.68 0.13 0.47 0.01

To extract diagonal elements of a matrix in R without using diag function, add the following code to the above snippet −

M8<-matrix(round(rexp(25,2.05),2),ncol=5)
M8[row(M8)==col(M8)]

## Output

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

[1] 0.60 0.40 0.79 1.08 0.01

Published on 11-Nov-2021 07:55:52