[R] a trick ??
Jonathan Rougier
J.C.Rougier at durham.ac.uk
Tue Feb 26 15:57:26 CET 2002
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
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list