Skip to content
Prev 146088 / 398500 Next

sum of unknown number of matrices

try a simple for loop, it will be fast enough in this case, e.g.,

matSums <- function (lis) {
    out <- array(data = 0, dim = dim(lis[[1]]))
    for (i in seq(along = lis))
        out <- out + lis[[i]]
    out
}

a <- b <- c <- d <- matrix(1:4, 2, 2)
l <- list(a, b ,c, d)

matSums(l)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, June 04, 2008 4:53 PM
Subject: [R] sum of unknown number of matrices
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm