I have matrices stored within a list like something as follows: a <- list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) b <- list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) I don't recall how to perform matrix multiplication on each list element such that the result is a new list result <- list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) I think I'm close with mapply(), but I'm doing something silly mapply('%*%', a,b) Thanks. Harold [[alternative HTML version deleted]]
On Tue, 2007-01-23 at 10:21 -0500, Doran, Harold wrote:> I have matrices stored within a list like something as follows: > > a <- list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) > b <- list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) > > I don't recall how to perform matrix multiplication on each list element > such that the result is a new list > > result <- list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) > > I think I'm close with mapply(), but I'm doing something silly > > mapply('%*%', a,b) > > Thanks. > HaroldHarold, That should basically be working. Just note that by default, each resultant matrix is put into a column (vector) format, rather than as a matrix. Res <- mapply("%*%", a, b) Res1 <- a[[1]] %*% b[[1]] Res2 <- a[[2]] %*% b[[2]]> str(Res)num [1:100, 1:2] 0.1713 0.8290 -0.0864 3.5420 -1.4638 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : NULL> all.equal(Res[, 1], as.vector(Res1))[1] TRUE> all.equal(Res[, 2], as.vector(Res2))[1] TRUE If you want the results to be a list of two matrices, you would do something like: Res <- mapply("%*%", a, b, SIMPLIFY = FALSE)> str(Res)List of 2 $ : num [1:10, 1:10] 0.1713 0.8290 -0.0864 3.5420 -1.4638 ... $ : num [1:10, 1:10] 0.220 -2.048 -0.135 -2.121 -0.399 ...> all.equal(Res1, Res[[1]])[1] TRUE> all.equal(Res2, Res[[2]])[1] TRUE HTH, Marc Schwartz
Try, mapply('%*%', a, b, SIMPLIFY=FALSE) -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Doran, Harold Sent: Tuesday, January 23, 2007 10:22 AM To: r-help at stat.math.ethz.ch Subject: [R] Matrix operations in a list I have matrices stored within a list like something as follows: a <- list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) b <- list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) I don't recall how to perform matrix multiplication on each list element such that the result is a new list result <- list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) I think I'm close with mapply(), but I'm doing something silly mapply('%*%', a,b) Thanks. Harold [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch 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.
you need the 'SIMPLIFY' argument of mapply(), i.e., mapply("%*%", a, b, SIMPLIFY = FALSE) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student 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: "Doran, Harold" <HDoran at air.org> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, January 23, 2007 4:21 PM Subject: [R] Matrix operations in a list>I have matrices stored within a list like something as follows: > > a <- list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) > b <- list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) > > I don't recall how to perform matrix multiplication on each list > element > such that the result is a new list > > result <- list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) > > I think I'm close with mapply(), but I'm doing something silly > > mapply('%*%', a,b) > > Thanks. > Harold > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm