Re: [R] Fast way to populate a sparse matrix

2014-04-29 Thread Tom Wright
Thanks everyone, That's a 3000 fold speedup. Now if only I can get the same improvement on the stMincuts iGraph algorithm. On Fri, Apr 25, 2014 at 4:20 AM, Martin Maechler wrote: > > Stefan Evert > > on Fri, 25 Apr 2014 09:09:31 +0200 writes: > > > On 24 Apr 2014, at 23:56, G

Re: [R] Fast way to populate a sparse matrix

2014-04-25 Thread Martin Maechler
> Stefan Evert > on Fri, 25 Apr 2014 09:09:31 +0200 writes: > On 24 Apr 2014, at 23:56, Greg Snow <538...@gmail.com> wrote: >> library(Matrix) >> >> adjM <- Matrix(0,nrow=10,ncol=10) >> >> locs <- cbind( sample(1:10), sample(1:10) ) >> vals <- rnorm(10)

Re: [R] Fast way to populate a sparse matrix

2014-04-25 Thread Stefan Evert
On 24 Apr 2014, at 23:56, Greg Snow <538...@gmail.com> wrote: > library(Matrix) > > adjM <- Matrix(0,nrow=10,ncol=10) > > locs <- cbind( sample(1:10), sample(1:10) ) > vals <- rnorm(10) > > adjM[ locs ] <- vals ... and once you've got your data in this format, why not construct the sparse ma

Re: [R] Fast way to populate a sparse matrix

2014-04-24 Thread Greg Snow
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: li

[R] Fast way to populate a sparse matrix

2014-04-24 Thread Tom Wright
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 3 x 3 element matrix