# Convert a single column matrix into a diagonal matrix in R.

R ProgrammingServer Side ProgrammingProgramming

A diagonal matrix is a type of square matrix that contains zero at non-diagonal elements starting from left-upper to right-bottom.

To convert a single column matrix into a diagonal matrix in R, we can use diag function along with matrix function and use ncol argument where we can put the number of columns equal to the number of values in the single column matrix.

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

## Example 1

Following snippet creates a sample matrix −

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

The following matrix is created −

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

To convert matrix M1 into a diagonal matrix on the above created matrix, add the following code to the above snippet −

M1<-matrix(rpois(5,2),ncol=1)
M1<-matrix(diag(as.vector(M1)),ncol=5)
M1

## Output

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

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

## Example 2

Following snippet creates a sample matrix −

M2<-matrix(rpois(5,500),ncol=1)
M2

The following matrix is created −

    [,1]
[1,] 505
[2,] 505
[3,] 523
[4,] 545
[5,] 507

To convert matrix M2 into a diagonal matrix on the above created matrix, add the following code to the above snippet −

M2<-matrix(rpois(5,500),ncol=1)
M2<-matrix(diag(as.vector(M2)),ncol=5)
M2

## Output

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

   [,1] [,2] [,3] [,4] [,5]
[1,] 505   0   0   0    0
[2,]   0 505   0   0    0
[3,]   0   0 523   0    0
[4,]   0   0   0 545    0
[5,]   0   0   0   0  507

## Example 3

Following snippet creates a sample matrix −

M3<-matrix(rnorm(5),ncol=1)
M3

The following matrix is created −

          [,1]
[1,]  0.2614596
[2,]  0.6914427
[3,] -1.8483160
[4,] -1.0057351
[5,] -1.4467553

To convert matrix M3 into a diagonal matrix on the above created matrix, add the following code to the above snippet −

M3<-matrix(rnorm(5),ncol=1)
M3<-matrix(diag(as.vector(M3)),ncol=5)
M3

## Output

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

          [,1]      [,2]      [,3]    [,4]       [,5]
[1,] 0.2614596 0.0000000  0.000000  0.000000  0.000000
[2,] 0.0000000 0.6914427  0.000000  0.000000  0.000000
[3,] 0.0000000 0.0000000 -1.848316 0.000000   0.000000
[4,] 0.0000000 0.0000000  0.000000 -1.005735  0.000000
[5,] 0.0000000 0.0000000  0.000000  0.000000 -1.446755

## Example 4

Following snippet creates a sample matrix −

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

The following matrix is created −

     [,1]
[1,] 26.6
[2,] 26.5
[3,] 22.1
[4,] 24.8
[5,] 19.1

To convert matrix M4 into a diagonal matrix on the above created matrix, add the following code to the above snippet −

M4<-matrix(round(rnorm(5,25,3.2),1),ncol=1)
M4<-matrix(diag(as.vector(M4)),ncol=5)
M4

## Output

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

    [,1] [,2] [,3] [,4]   [,5]
[1,] 26.6 0.0  0.0  0.0    0.0
[2,] 0.0 26.5  0.0  0.0    0.0
[3,] 0.0  0.0 22.1  0.0    0.0
[4,] 0.0  0.0  0.0 24.8    0.0
[5,] 0.0  0.0  0.0  0.0   19.1

## Example 5

Following snippet creates a sample matrix −

M5<-matrix(round(runif(5,2,10),2),ncol=1)
M5


The following matrix is created −

     [,1]
[1,] 5.92
[2,] 7.34
[3,] 7.72
[4,] 3.18
[5,] 7.00

To convert matrix M5 into a diagonal matrix on the above created matrix, add the following code to the above snippet −

M5<-matrix(round(runif(5,2,10),2),ncol=1)
M5<-matrix(diag(as.vector(M5)),ncol=5)
M5

## Output

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

     [,1] [,2] [,3] [,4] [,5]
[1,] 5.92 0.00 0.00 0.00  0
[2,] 0.00 7.34 0.00 0.00  0
[3,] 0.00 0.00 7.72 0.00  0
[4,] 0.00 0.00 0.00 3.18  0
[5,] 0.00 0.00 0.00 0.00  7
Published on 08-Nov-2021 11:44:42