[R] Fast way to populate a sparse matrix

Martin Maechler maechler at stat.math.ethz.ch
Fri Apr 25 10:20:45 CEST 2014

>>>>> Stefan Evert <stefanML at collocations.de>
>>>>>     on Fri, 25 Apr 2014 09:09:31 +0200 writes:

    > 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

More information about the R-help mailing list