[R] Fast way to populate a sparse matrix

Greg Snow 538280 at gmail.com
Thu Apr 24 23:56:28 CEST 2014


Convert your 'targets' matrix into a 2 column matrix with the 1st
column representing the row and the 2nd the column where you want your
values, then change the values to a single vector and you can just use
the targets matrix as the subsetting in 1 step without (explicit)
looping, for example:

library(Matrix)

adjM <- Matrix(0,nrow=10,ncol=10)

locs <- cbind( sample(1:10), sample(1:10) )
vals <- rnorm(10)

adjM[ locs ] <- vals

I would expect this to be faster than looping (but have not tested).

On Thu, Apr 24, 2014 at 9:45 AM, Tom Wright <tom at maladmin.com> wrote:
> 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 at 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com




More information about the R-help mailing list