R fast matrix formulation

Viewed 87

I need a fast way in R to insert a matrix into another matrix in R. My current matrix shape is block diagonal shaped like this:

library(Matrix)
block1  <- new("dgCMatrix" , i = c(0L, 1L, 2L, 0L, 1L, 2L, 0L, 1L, 2L, 3L, 4L, 
           5L, 3L, 4L, 5L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 6L, 7L, 8L, 
           9L, 6L, 7L, 8L, 9L, 6L, 7L, 8L, 9L) , 
   p = c(0L, 3L, 6L, 9L, 12L, 15L, 18L, 22L, 26L, 30L, 34L) , 
   Dim = c(10L, 10L) , Dimnames = list(NULL, NULL) , 
   x = c(1.002, 0.002, 0, 0.002, 1.002, 0.002, 0, 0.002, 1.002,
         1.002, 0.002, 0, 0.002, 1.002, 0.002, 0, 0.002, 1.002, 
         3.002, 1.924, 0.507, 0.055, 1.924, 3.002, 1.924, 
         0.507, 0.507, 1.924, 3.002, 1.924, 0.055, 0.507, 1.924, 3.002) , 
   factors = list())

I want to insert the matrix below into the elements [4:10,1:6] of the matrix above.

block2 <- 
  structure(c(0.779, 0.018, 0, 0.779, 0.018, 0, 0.256, 0.169, 0, 0.256, 
            0.169, 0, 0.035, 0.641, 0, 0.035, 0.641, 0, 0.002, 1, 
            0.002, 0.002, 1, 0.002), .Dim = c(6L, 4L))

I am currenly doing so using:

block1[1:6,7:10]=block2

However this takes a lot of time if my matrix is huge. Is there any other function or clever method I can use?

0 Answers
Related