Re: [R] averaging a list of matrices element wise

2012-11-06 Thread Bert Gunter
f some data and an aching desire for an answer does not > ensure that a reasonable answer can be extracted from a given body of data. > ~ John Tukey > > -Oorspronkelijk bericht- > Van: Bert Gunter [mailto:gunter.ber...@gene.com] > Verzonden: maandag 5 november 2012 16:13 > Aan:

Re: [R] averaging a list of matrices element wise

2012-11-06 Thread ONKELINX, Thierry
ey -Oorspronkelijk bericht- Van: Bert Gunter [mailto:gunter.ber...@gene.com] Verzonden: maandag 5 november 2012 16:13 Aan: D. Rizopoulos CC: ONKELINX, Thierry; r-help@r-project.org Onderwerp: Re: [R] averaging a list of matrices element wise Gents: Although it is difficult to say what may be

Re: [R] averaging a list of matrices element wise

2012-11-05 Thread Bert Gunter
Gents: Well... (munch munch) I do think I must eat my words: >z <- lapply(seq_len(1e6),function(x)matrix(runif(100),nr=10)) > system.time(Reduce("+",z)/length(z)) user system elapsed 3.480.053.52 > system.time(rowMeans(array(unlist(z),dim=c(10,10,length(z))),dims=2)) user syst

Re: [R] averaging a list of matrices element wise

2012-11-05 Thread Bert Gunter
Gents: Although it is difficult to say what may be faster, as it typically depends on the data, and it is even more difficult to say what is fast enough, I suspect that ?rowMeans ## specifically written for speed would be considerably faster than Reduce (or an apply() )approach on the array), b

Re: [R] averaging a list of matrices element wise

2012-11-05 Thread arun
"r-help@r-project.org" Cc: Sent: Monday, November 5, 2012 5:32 AM Subject: [R] averaging a list of matrices element wise Dear all, I have a list of n matrices which all have the same dimension (r x s). What would be a fast/elegant way to calculate the element wise average? So result[1, 1] <

Re: [R] averaging a list of matrices element wise

2012-11-05 Thread D. Rizopoulos
If you don't have any NAs, then one way is: n <- 3 r <- 5 s <- 6 raw <- lapply(seq_len(n), function(i){ matrix(rnorm(r * s), ncol = r) }) Reduce("+", raw) / length(raw) I hope it helps. Best, Dimitris On 11/5/2012 11:32 AM, ONKELINX, Thierry wrote: > Dear all, > > I have a list of n matri

[R] averaging a list of matrices element wise

2012-11-05 Thread ONKELINX, Thierry
Dear all, I have a list of n matrices which all have the same dimension (r x s). What would be a fast/elegant way to calculate the element wise average? So result[1, 1] <- mean(c(raw[[1]][1, 1] , raw[[2]][1, 1], raw[[...]][1, 1], raw[[n]][1, 1])) Here is my attempt. #create a dummy dataset n <