I need to generate a sparse matrix. Currently I have the data held in two regular matrices. One 'targets' holds the column subscripts while the other 'scores' holds the values. I have written a 'toy' sample below. Using this approach takes about 90 seconds to populate a 30000 x 30000 element matrix. I'm going to need to scale this up by a factor of about 1000 so I really need a faster way of populating the sparse matrix. Any advice received gratefully.
# toy code starts here require('Matrix') set.seed(0) adjM<-Matrix(0,nrow=10,ncol=10) #generate the scores for the sparse matrix, with the target locations targets<-matrix(nrow=10,ncol=5) scores<-matrix(nrow=10,ncol=5) for(iloc in 1:10) { targets[iloc,]<-sample(1:10,5,replace=FALSE) scores[iloc,]<-rnorm(5) } #populate the sparse matrix for(iloc in 1:10) { adjM[iloc,targets[iloc,!is.na(targets[iloc,])]]<-scores[iloc,!is.na (targets[iloc,])] } [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.