On Mon, 3 Jan 2005, Doran, Harold wrote:
Dear List:
I am having to build a block-diagonal matrix (vl) and am currently using
the following code.
I<-diag(sample.size)
vl<-kronecker(I,vl.mat)
This code works fine, but for large N, it is a huge memory hog. Is there
a more efficient method for constructing vl?
Obvious alternatives such as
nr<-nrow(v1.mat)
nc<-ncol(v1.mat)
result<-matrix(0,nrow=sample.size*nr,ncol=sample.size*nc)
for(i in 1:sample.size){
result[ (i-1)*nr+1:nr, (i-1)*nc+1:nc]<-v1.mat
}
or
nr<-nrow(v1.mat)
nc<-ncol(v1.mat)
iy<-as.vector(outer(rep(1:nc,each=nr),(0:(sample.size-1))*nc,"+"))
ix<-as.vector(outer(rep(1:nr, nc),(0:(sample.size-1))*nr,"+"))
result<-matrix(0,nrow=sample.size*nr,ncol=sample.size*nc)
result[cbind(ix,iy)]<-v1.mat
seem to take a little less memory and about the same time,
but constructing a large block-diagonal matrix is intrinsically an
inefficient thing to do.
-thomas
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html