Re: [R] matrix size

From: Martin Maechler <>
Date: Mon 01 Jan 2007 - 23:18:45 GMT

>>>>> "GArmelini" == Armelini, Guillermo <>
>>>>> 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]

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.

Martin Maechler, ETH Zurich mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue Jan 02 10:24:26 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 01 Jan 2007 - 23:30:28 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.