# How to create a block diagonal matrix using a matrix in R?

To create a block diagonal matrix using a matrix in R, we can use bdiag function of Matrix package.

For Example, if we have a matrix called M and we want to create block diagonal using M 4 times by using the below command −

bdiag(replicate(4,M,simplify=FALSE))

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

## Example 1

Following snippet creates a sample matrix −

M1<-matrix(rpois(9,5),ncol=3)
M1

The following matrix is created −

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

To create a block diagonal matrix using a matrix in R on the above created matrix, add the following code to the above snippet −

M1<-matrix(rpois(9,5),ncol=3)
library(Matrix)
bdiag(replicate(3,M1,simplify=FALSE))

## Output

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

9 x 9 sparse Matrix of class "dgCMatrix"
[1,] 9 3 10 . . . . . .
[2,] 4 4  2 . . . . . .
[3,] 5 8  2 . . . . . .
[4,] . . . 9 3 10 . . .
[5,] . . . 4 4  2 . . .
[6,] . . . 5 8  2 . . .
[7,] . . . . . . 9 3 10
[8,] . . . . . . 4 4  2
[9,] . . . . . . 5 8  2

## Example 2

Following snippet creates a sample matrix −

M2<-matrix(rpois(4,2),ncol=2)
M2

The following matrix is created −

  [,1] [,2]
[1,] 1  2
[2,] 4  1

To create a block diagonal matrix using a matrix in R on the above created matrix, add the following code to the above snippet −

M2<-matrix(rpois(4,2),ncol=2)
library(Matrix)
bdiag(replicate(6,M2,simplify=FALSE))

## Output

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

12 x 12 sparse Matrix of class "dgCMatrix"

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

## Example 3

Following snippet creates a sample matrix −

M3<-matrix(round(rnorm(16),2),ncol=4)
M3

The following matrix is created −

      [,1]  [,2] [,3]  [,4]
[1,] -0.61 -1.41  0.46  0.67
[2,] -0.08 -0.69 -0.92 -0.61
[3,] -0.44 -0.48 -0.09 -0.42
[4,] -1.79  0.45  1.34 -0.76

To create a block diagonal matrix using a matrix in R on the above created matrix, add the following code to the above snippet −

M3<-matrix(round(rnorm(16),2),ncol=4)
library(Matrix)
bdiag(replicate(2,M3,simplify=FALSE))

## Output

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

8 x 8 sparse Matrix of class "dgCMatrix"

[1,] -0.61 -1.41  0.46  0.67 . . . .
[2,] -0.08 -0.69 -0.92 -0.61 . . . .
[3,] -0.44 -0.48 -0.09 -0.42 . . . .
[4,] -1.79  0.45  1.34 -0.76 . . . .
[5,]   .    .     .       . -0.61 -1.41  0.46  0.67
[6,]   .    .     .       . -0.08 -0.69 -0.92 -0.61
[7,]   .    .     .       . -0.44 -0.48 -0.09 -0.42
[8,]   .    .     .       . -1.79  0.45  1.34 -0.76

## Example 4

Following snippet creates a sample matrix −

M4<-matrix(round(rnorm(9),2),ncol=3)
M4

The following matrix is created −

      [,1] [,2] [,3]
[1,] -0.11  1.49  0.86
[2,] -0.06 -0.39  0.96
[3,]  1.29 -2.67 -0.03

To create a block diagonal matrix using a matrix in R on the above created matrix, add the following code to the above snippet −

M4<-matrix(round(rnorm(9),2),ncol=3)
library(Matrix)
bdiag(replicate(3,M4,simplify=FALSE))

## Output

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

9 x 9 sparse Matrix of class "dgCMatrix"

[1,] -0.11  1.49  0.86 . . . . . .
[2,] -0.06 -0.39  0.96 . . . . . .
[3,]  1.29 -2.67 -0.03 . . . . . .
[4,]   .    .     . -0.11  1.49  0.86 .   .   .
[5,]   .    .     . -0.06 -0.39  0.96 .   .   .
[6,]   .    .     .  1.29 -2.67 -0.03 .   .   .
[7,]   .    .     .    .   .     . -0.11  1.49  0.86
[8,]   .    .     .    .   .     . -0.06 -0.39  0.96
[9,]   .    .     .    .   .     .  1.29 -2.67 -0.03

## Example 5

Following snippet creates a sample matrix −

M5<-matrix(rpois(4,10),ncol=2)
M5

The following matrix is created −

   [,1] [,2]
[1,] 10  6
[2,] 5  17

To create a block diagonal matrix using a matrix in R on the above created matrix, add the following code to the above snippet −

M5<-matrix(rpois(4,10),ncol=2)
library(Matrix)
bdiag(replicate(5,M5,simplify=FALSE))

## Output

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

10 x 10 sparse Matrix of class "dgCMatrix"

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

Updated on: 03-Nov-2021

1K+ Views