Skip to content
Prev 309892 / 398502 Next

averaging a list of matrices element wise

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), but I have **not** checked. I am of course prepared to eat
my words in the face of data to the contrary.

The call would be:

result <- rowMeans( array(unlist(raw), dim = c(r,s,length(raw)), dims=2)

Note that rowMeans() has an na.rm arguments to handle NA's. See the
help file for deatils.
Note also the tradeoff to memory, as copies of raw probably are made
during evaluation.
Finally note that dimnames are lost in the final result, so the above
would have to be followed by

dimnames(result) <- dimnames(raw[[1]])

to get them back.

-- Bert
On Mon, Nov 5, 2012 at 2:43 AM, D. Rizopoulos <d.rizopoulos at erasmusmc.nl> wrote: