在 R 中 "corner" 处合并矩阵?

Combine matrices at "corner" in R?

有没有针对一般情况的简单方法?

# Combine two (or more) matrices
m1 <- matrix(c(1, .5, .5, 1), nrow = 2)
m1
m2 <- matrix(c(.5, .25, .25, .5), nrow =2)
m2

# Such that they end up combined as unique rows/columns, like so?

m_combined <- 
  matrix(c(1, .5, 0, 0, .5, 1, 0, 0,
           0, 0, .5, .25, 0, 0, .25, .5),
         nrow = 4)
m_combined

我们可以使用bdiag

library(Matrix)
as.matrix(bdiag(m1, m2))

-输出

     [,1] [,2] [,3] [,4]
[1,]  1.0  0.5 0.00 0.00
[2,]  0.5  1.0 0.00 0.00
[3,]  0.0  0.0 0.50 0.25
[4,]  0.0  0.0 0.25 0.50