Repeating a block matrix many times in diagonal part of a block matrix, with the off-diagonal blocks all zero matrices?

Viewed 1607

I would like to create a block diagonal matrix with the diagonal blocks being repeated a certain number of times, with the off-diagonal blocks being all zero matrices. For example, suppose we start with a matrix with the following:

> diag.matrix

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

I would like this matrix to be the diagonal block matrix so that in the end I have something like:

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

Here, we have the same block matrix above repeated twice in the block diagonals. If I wanted to do this efficiently an arbitrary number of times, is there a way to do it? thanks!

1 Answers
Related