Skip to content

Fast way to populate a sparse matrix

5 messages · Greg Snow, Stefan Evert, Martin Maechler +1 more

#
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:

  
    
#
On 24 Apr 2014, at 23:56, Greg Snow <538280 at gmail.com> wrote:

            
... and once you've got your data in this format, why not construct the sparse matrix directly?

	adjM <- sparseMatrix(i = locs[,1], j = locs[,2], x = vals)

I've found this to be very efficient and have used it with sparse matrices containing up to around 100 million nonzero entries.

Hope this helps,
Stefan
#

        
> On 24 Apr 2014, at 23:56, Greg Snow <538280 at 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 matrix directly?

    > adjM <- sparseMatrix(i = locs[,1], j = locs[,2], x = vals)

    > I've found this to be very efficient and have used it with sparse matrices containing up to around 100 million nonzero entries.

Yes, indeed,  thank you Stefan!

Let me reiterate :

__  Unless you can use special constructors such as  
__
__   Diagonal()      # diagonal matrices
__   bdiag()         # block diagonal matrices
__   bandSparse()    # "banded diagonal" matrices
__   kronecker(a, b) # where a or b are sparse
__
__   (and maybe another one I forgot)
__
__  *the* way to efficiently construct large sparse matrices is
__    sparseMatrix()
__    or sometimes its alternative / precursor  spMatrix().
__
__    Matrix(d, ...., sparse=TRUE)
__
__  is nice and fine only for relatively *small* matrices,
__  as it really works from a dense original 'd' (directly or via replication)


    > Hope this helps,

I do hope, too.
I'm very happy for suggestions on  how  we as Matrix authors
could make this better known.

Recently, someone proposed to make the 'rsparseMatrix()'
utility function from  help(sparseMatrix)
into an "official" Matrix package function.  If I did that,
I could start using  rsparseMatrix() in typical examples rather
than the current often use of  Matrix()  or
as(<traditional matrix>, "sparseMatrix")
both of which are perfect for the small examples that are
typical for help files.

Martin Maechler,
ETH Zurich
4 days later