How to set the diagonal elements of a matrix to 1 in R?


First thing we need to understand is diagonal elements are useful only if we have a square matrix, otherwise it would not make sense to set diagonal elements, this is known to almost all mathematicians but some freshman might get confused because we can create diagonal in a non-square matrix which should not be called a diagonal. In R, we can set the diagonal elements of a matrix to 1 by using diag function.

Example1

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
> diag(M1)<-1
> M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1    6   11   16   21
[2,] 2    1   12   17   22
[3,] 3    8    1   18   23
[4,] 4    9   14    1   24
[5,] 5   10   15   20    1

Example2

Live Demo

> M2<-matrix(rpois(36,5),ncol=6)
> M2

Output

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5    4    4    5    6    5
[2,] 4    5    7    8    3    4
[3,] 4    3    6    5    5    6
[4,] 7    4    4    6    2    6
[5,] 2    5    2    4    6    6
[6,] 7    2    3    5    4    3
> diag(M2)<-1
> M2

Output

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

Example3

Live Demo

> M3<-matrix(rpois(64,10),ncol=8)
> M3

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 8 9 17 7 12 11 4 10
[2,] 8 15 8 7 13 10 13 11
[3,] 13 9 9 13 4 10 11 9
[4,] 9 9 7 10 5 13 11 5
[5,] 7 9 9 8 9 11 8 15
[6,] 12 8 10 14 15 3 4 8
[7,] 9 8 7 11 11 11 10 6
[8,] 7 12 6 11 10 6 9 7
> diag(M3)<-1
> M3

Output

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

Example4

Live Demo

> M4<-matrix(sample(1:10,100,replace=TRUE),ncol=10)
> M4

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 4 8 8 1 8 6 1 4 10 7
[2,] 4 8 3 4 8 5 10 3 10 9
[3,] 8 2 1 7 4 7 7 9 3 5
[4,] 6 2 9 2 10 2 2 2 6 4
[5,] 6 9 4 8 2 1 8 4 7 7
[6,] 2 9 7 9 1 6 10 8 7 9
[7,] 3 6 2 7 5 2 7 3 1 5
[8,] 1 6 6 10 2 7 6 2 1 9
[9,] 1 1 6 3 10 1 4 4 9 3
[10,] 7 10 3 7 10 10 4 10 1 9
> diag(M4)<-1
> M4

Output

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

Example5

Live Demo

> M5<-matrix(sample(0:9,25,replace=TRUE),ncol=5)
> M5

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 6    6    9    9    0
[2,] 2    4    3    9    7
[3,] 1    7    8    3    8
[4,] 9    5    0    4    6
[5,] 9    7    0    1    3
> diag(M5)<-1
> M5

Output

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

Updated on: 06-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements