[R] Direct sum of matrices

Gabor Grothendieck ggrothendieck at myway.com
Fri Jan 28 21:42:40 CET 2005


Kenneth <krcabrer <at> unalmed.edu.co> writes:

: 
: Hi R users:
: 
: How can I built a direct sum function of matrices in R?
: 
: I mean A(mxn), B(pxq), C(rxs),...
: 
: X<-ds(A,B,C,...)
: 
: X = [ A,  0,  0
:             0,  B,  0
:            0,   0,  C] ((m+p+r+...) x (n+q+s+...))
: 
: Thank you for your help.

Search r-help for blockdiag, adiag or bdiag.  

At that time I had posted a solution using zoo but the fill=
argument in merge.zoo has been added since then so here is an
update that's slightly shorter.  Suppose we have

m1 <- matrix(1:4, 2, 2)
m2 <- matrix(11:14, 2, 2)

# Then we create two time series with non-overlapping time
# intervals and merge them using a fill of zero.  You can
# use any number of zoo matrices in zoo, but here we just use two:

m <- merge( zoo(m1, 1:2), zoo(m2, 3:4), fill = 0)

# You may wish to clean the result up a bit with:

class(m) <- "matrix"; dimnames(m) <- NULL




More information about the R-help mailing list