Hello, Using the technique suggested here ( https://stat.ethz.ch/pipermail/r-devel/2007-September/047028.html), we can update a matrix in place (see example 1 below), with huge time and memory savings.
Is it possible to tweak example 2 to update in place also? The only difference is that the matrix is held within a new environment. Thanks, Simon # example 1 - fast and doesn't create copies rm(list=ls()) main <- function() { matrix1 <- matrix(1.0, nrow=60000, ncol=200) if (sum(matrix1) == 12000000) { print ("OK") } else { print ("NOT OK") } funcExample <- function() eval.parent(substitute({ print(paste("Mem1", memory.size())) for (i in 1:30) { eval(parse(text="temp <- matrix1[10,]")) eval(parse(text="matrix1[10,] <- temp * 0")) } print(paste("Mem2", memory.size())) })) print (paste("Time:", system.time(funcExample())[1], "secs")) if (sum(matrix1) == 11999800) { print ("OK") } else { print ("NOT OK") } } main() [1] "OK" [1] "Mem1 99.32" [1] "Mem2 99.46" [1] "Time: 0 secs" [1] "OK" # example 2 - slow and does create copies rm(list=ls()) main <- function() { envData <- new.env() envData$matrix1 <- matrix(1.0, nrow=60000, ncol=200) if (sum(envData$matrix1) == 12000000) { print ("OK") } else { print ("NOT OK") } funcExample <- function() eval.parent(substitute({ print(paste("Mem1", memory.size())) for (i in 1:30) { eval(parse(text="temp <- envData$matrix1[10,]")) eval(parse(text="envData$matrix1[10,] <- temp * 0" )) } print(paste("Mem2", memory.size())) })) print (paste("Time:", system.time(funcExample())[1], "secs")) if (sum(envData$matrix1) == 11999800) { print ("OK") } else { print ("NOT OK") } } main() [1] "OK" [1] "Mem1 99.32" [1] "Mem2 190.88" [1] "Time: 2.6 secs" [1] "OK" ************************************************************ HSBC Global Asset Management (UK) Limited 78 St James's Street, London SW1A 1EJ Telephone: 020 7991 8888 Fax: 020 7024 1999 Registered Office: 8 Canada Square, London E14 5HQ, United Kingdom Registered in England number 1917956 Authorised and regulated by the Financial Services Authority ************************************************************ ----------------------------------------- SAVE PAPER - THINK BEFORE YOU PRINT! This E-mail is confidential. It may also be legally privileged. If you are not the addressee you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return E-mail. Internet communications cannot be guaranteed to be timely secure, error or virus-free. The sender does not accept liability for any errors or omissions. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.