- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

- Related Questions & Answers
- How to convert a matrix into a matrix with single column in R?
- How to convert a vector into a diagonal matrix in R?
- How to convert a matrix column into list in R?
- How to convert a sparse matrix into a matrix in R?
- How to convert a matrix into a color matrix in R?
- How to convert diagonal elements of a matrix in R into missing values?
- How to convert character column of a matrix into numeric in R?
- Program to convert given Matrix to a Diagonal Matrix in C++
- How to create a block diagonal matrix using a matrix in R?
- How to convert a vector into matrix in R?
- How to convert a table into matrix in R?
- How to convert a text vector into a matrix in R?
- How to convert data.table object into a matrix in R?
- How to convert matrix rows into a list in R?
- How to convert an array into a matrix in R?