sandeep
sandeep

Reputation: 83

Form a block diagonal matrix in r

I would like to form a 100x100 block diagonal matrix whose diagonal entities are 5, ((2k-1)2k) and (2k(2k-1)) entities are the same as 0.5 for k = 1,2,...50, and all other entities are zero.

Upvotes: 0

Views: 576

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101373

With base R, you can achieve it via using kronecker() to creat block-diagonal matrix, i.e.,

M <- kronecker(diag(50),matrix(c(5,0.5,0.5,5),2,2))

Upvotes: 3

sandeep
sandeep

Reputation: 83

> library(Matrix)
> M <- as.matrix(bdiag(lapply(seq_len(50), function(X) matrix(c(5, 0.5, 0.5, 5), 2))))
> dim(M)

Upvotes: 1

Related Questions