[R] a trick ??

Jonathan Rougier
Tue, 26 Feb 2002 14:57:26 -0000

Olivier Martin wrote:
>
> Dear R users,
>
> Suppose i have an A square matrix rxr. I want to obtain a block
> matrix  B (pxr,pxr) where
> the p diagonal blocks are A and  the others values are 0.
> I would like to do something like : diag(A,ncol=pr, nrow=pr)
> How can i do it ??
> Thanks in advance,
> Olivier.

I think you want "kronecker", eg

> A <- matrix(1:4, 2, 2)
> kronecker(diag(1, 3), A)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    0    0    0    0
[2,]    2    4    0    0    0    0
[3,]    0    0    1    3    0    0
[4,]    0    0    2    4    0    0
[5,]    0    0    0    0    1    3
[6,]    0    0    0    0    2    4
> diag(1, 3) %x% A # short form
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    0    0    0    0
[2,]    2    4    0    0    0    0
[3,]    0    0    1    3    0    0
[4,]    0    0    2    4    0    0
[5,]    0    0    0    0    1    3
[6,]    0    0    0    0    2    4

Cheers, Jonathan.
--
Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388