How to combine two matrices to create a block-diagonal matrix in R?


A block-diagonal matrix means that a matrix added to another matrix at the end the last element. For example, if we have a matrix with nine values and the other matrix also has nine values then the second matrix will be added to the first matrix and the elements below first matrix will be zero and the elements above the second matrix will also be zero.

Example

 Live Demo

M1<-matrix(1:9,ncol=3)
M1

Output

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

Example

 Live Demo

M2<-matrix(10:18,ncol=3)
M2

Output

    [,1] [,2] [,3]
[1,] 10   13   16
[2,] 11   14   17
[3,] 12   15   18

Example

rbind(cbind(M1,matrix(0,nrow=nrow(M1),ncol=ncol(M2))),cbind(matrix(0,nrow=nrow(M2),ncol=ncol(M1)),M2))

Output

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

Example

 Live Demo

M3<-matrix(1:25,nrow=5)
M3

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

Example

 Live Demo

M4<-matrix(26:50,ncol=5)
M4

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 26 31 36 41 46
[2,] 27 32 37 42 47
[3,] 28 33 38 43 48
[4,] 29 34 39 44 49
[5,] 30 35 40 45 50

Example

rbind(cbind(M3,matrix(0,nrow=nrow(M4),ncol=ncol(M4))),cbind(matrix(0,nrow=nrow(M4),ncol=ncol(M3)),M4))

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1   6    11   16   21   0 0 0 0 0
[2,] 2   7    12   17   22   0 0 0 0 0
[3,] 3   8    13   18   23   0 0 0 0 0
[4,] 4   9    14   19   24   0 0 0 0 0
[5,] 5   10   15   20   25   0 0 0 0 0
[6,] 0   0    0    0    0    26 31 36 41 46
[7,] 0   0    0    0    0   27 32 37 42 47
[8,] 0   0    0    0    0     28 33 38 43 48
[9,] 0   0    0    0    0     29 34 39 44 49
[10,] 0   0  0    0    0     30 35 40 45 50

Example

 Live Demo

M5<-matrix(sample(1:5,25,replace=TRUE),nrow=5)M5

Output

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

Example

 Live Demo

M6<-matrix(sample(6:10,25,replace=TRUE),ncol=5)
M6

Output

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

Example

rbind(cbind(M5,matrix(0,nrow=nrow(M6),ncol=ncol(M6))),cbind(matrix(0,nrow=nrow(M6),ncol=ncol(M5)),M6))

Output

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

Example

 Live Demo

M7<-matrix(round(rnorm(36),0),nrow=6)
M7

Output

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

Example

 Live Demo

M8<-matrix(round(runif(36,2,5),0),nrow=6)
M8

Output

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

Example

rbind(cbind(M7,matrix(0,nrow=nrow(M8),ncol=ncol(M8))),cbind(matrix(0,nrow=nrow(M8),ncol=ncol(M7)),M8))

Output

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

Updated on: 10-Oct-2020

529 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements