[R] matrix size

Martin Maechler maechler at stat.math.ethz.ch
Tue Jan 2 00:18:45 CET 2007


>>>>> "GArmelini" == Armelini, Guillermo <DOCGArmelini at iese.edu>
>>>>>     on Mon, 1 Jan 2007 23:43:31 +0100 writes:

    GArmelini> Hello everyone Could anybody tell me how to set
    GArmelini> the following matrix?
 
    GArmelini> n2<-matrix(nrow=10185,ncol=10185,seq(0,0,length=103734225))
 
    GArmelini> R answer was Error: cannot allocate vector of
    GArmelini> size 810423 Kb

of course.
 
    GArmelini> Are there any solution? I tried to increase the
    GArmelini> memory size but it didn't work G

You might consider using *sparse* matrices
such as available by R's "Matrix" package.

 install.packages("Matrix")  # once only
 library(Matrix)             # every time you use it
 n2 <- Matrix(0, nrow=10185, ncol=10185)

will produce a sparse Matrix (of class "dsCMatrix")
that does not need a lot of memory.

If you want to do anything reasonable, with 'n2' I assume you'll
want to add some non-zero entries.

To do that (and similary things) a bit more efficiently in the 
current implementations of "Matrix", 
I'd recommend to work with a "TsparseMatrix"
(instead of the `default' Csparse*), i.e. a sparse matrix
representation working with so called triplets, e.g.

m2 <- as(n2, "TsparseMatrix")
m2[1,3] <- 10
m2[2, 1:10] <- 0:9

m2[1:10, 1:20]

etc,.. 
BTW: Such sub-assignments should now work, but some or still
slow. Till now the main emphasis for such matrices was general
good organization and speed in matrix operations rather than
"index operations".

I'm quite interested to hear what you want to do with your
matrix.  Use R-help if you think it could be of general
interest, or reply privately if you prefer.

Regards,
Martin Maechler, ETH Zurich



More information about the R-help mailing list