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. -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- Olivier MARTIN PhD student phone: (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail:olivier.martin at inrialpes.fr 38334 Saint Ismier cedex -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20020226/a40c68e1/attachment.html
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
diag(rep(diag(A),p)) ,of course. (If I understand what you mean) cheers, vito ----- Original Message ----- From: Olivier Martin To: r-news Sent: Tuesday, February 26, 2002 1:09 PM Subject: [R] a trick ?? 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. -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- Olivier MARTIN PhD student phone: (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail:olivier.martin at inrialpes.fr 38334 Saint Ismier cedex -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
A more general solution (given a list of matrices [with any
dimensions] blockdiag(a1,a2,a3) gives the block diagonal concatenation of
the matrices). I don't know if "kronecker" can do this or not.
blockdiag <- function(...) {
args <- list(...)
nr <- sapply(args,nrow)
nc <- sapply(args,ncol)
cumnc <- cumsum(nc)
NR <- sum(nr)
NC <- sum(nc)
rowfun <- function(m,zbefore,zafter) {
cbind(matrix(0,ncol=zbefore,nrow=nrow(m)),m,
matrix(0,ncol=zafter,nrow=nrow(m)))
}
ret <- rowfun(args[[1]],0,NC-ncol(args[[1]]))
for (i in 2:length(args)) {
ret <- rbind(ret,rowfun(args[[i]],cumnc[i-1],NC-cumnc[i]))
}
ret
}
On Tue, 26 Feb 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.
>
> --
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> Olivier MARTIN
> PhD student phone: (33) 04 76 61 53 55
> Projet IS2 06 08 67 93 42
> INRIA Rhone-Alpes fax : (33) 04 76 61 54 77
> 655, Av. de l'Europe
> Montbonnot e-mail:olivier.martin at inrialpes.fr
> 38334 Saint Ismier cedex
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>
>
>
--
318 Carr Hall bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._