[R] Memory Efficient Methods for Building Matrix

Thomas Lumley tlumley at u.washington.edu
Mon Jan 3 18:54:01 CET 2005


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




More information about the R-help mailing list