简体   繁体   English

从 R 中的较小矩阵创建矩阵

[英]Create matrix from smaller matrices in R

Is there a general function to build a matrix from smaller blocks, ie build matrix是否有从较小的块构建矩阵的通用函数,即构建矩阵

A B
C D

from matrices A, B, C, D?来自矩阵 A、B、C、D?

Of course there is this obvious way to create an empty big matrix and use sub-indexing, but isn't there anything simpler, easier and possibly faster?当然,有这种明显的方法可以创建一个空的大矩阵并使用子索引,但是没有什么更简单、更容易和可能更快的方法了吗?

Here are some base R solutions.这里有一些基本的 R 解决方案。 Maybe you can use也许你可以用

M <- rbind(cbind(A,B),cbind(C,D))

or或者

u <- list(list(A,B),list(C,D))
M <- do.call(rbind,Map(function(x) do.call(cbind,x),u))

Example例子

A <- matrix(1:4,nrow = 2)
B <- matrix(1:6,nrow = 2)
C <- matrix(1:6,ncol = 2)
D <- matrix(1:9,nrow = 3)

such that以至于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM