Block-diagonal binding of matrices

adiag from a package magic does what you want:

library(magic)
adiag(a,b)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    0    0
[2,]    2    4    6    0    0
[3,]    0    0    0    7    9
[4,]    0    0    0    8   10

Alternatively, you could use a package Matrix and function bdiag

library(Matrix)
bdiag(a,b)
4 x 5 sparse Matrix of class "dgCMatrix"

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

that returns a sparse matrix and which might be more efficient. Use as.matrix(bdiag(a,b)) to get a regular one.

Leave a Comment