Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 −
M1The following matrix is created −
[,1] [1,] 2 [2,] 3 [3,] 5 [4,] 1 [5,] 1To convert matrix M1 into a diagonal matrix on the above created matrix, add the following code to the above snippet −
M1Output
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 1Example 2
Following snippet creates a sample matrix −
M2The following matrix is created −
[,1] [1,] 505 [2,] 505 [3,] 523 [4,] 545 [5,] 507To convert matrix M2 into a diagonal matrix on the above created matrix, add the following code to the above snippet −
M2Output
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 507Example 3
Following snippet creates a sample matrix −
M3The following matrix is created −
[,1] [1,] 0.2614596 [2,] 0.6914427 [3,] -1.8483160 [4,] -1.0057351 [5,] -1.4467553To convert matrix M3 into a diagonal matrix on the above created matrix, add the following code to the above snippet −
M3Output
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.446755Example 4
Following snippet creates a sample matrix −
M4The following matrix is created −
[,1] [1,] 26.6 [2,] 26.5 [3,] 22.1 [4,] 24.8 [5,] 19.1To convert matrix M4 into a diagonal matrix on the above created matrix, add the following code to the above snippet −
M4Output
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.1Example 5
Following snippet creates a sample matrix −
M5The following matrix is created −
[,1] [1,] 5.92 [2,] 7.34 [3,] 7.72 [4,] 3.18 [5,] 7.00To convert matrix M5 into a diagonal matrix on the above created matrix, add the following code to the above snippet −
M5Output
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
