Hi, Suppose that I have a list where each component is a list of two matrices. I also have a vector of weights. How can I collapse my list of lists into a single list of two matrices where each matrix in the result is the weighted sum of the corresponding matrices. I could use a loop but this is a nested calculation so I was hoping there is a more efficient way to do this. To help clarify, here is the code I would use with a for loop result <- list(mat1=matrix(0,nrow1,ncol1), mat2=matrix(0,nrow2,ncol2)) for (i in seq(along=matlist)) { result$mat1 <- result$mat1+w[i]*matlist[[i]]$mat1 result$mat2 <- result$mat2+w[i]*matlist[[i]]$mat2 } I apologise if this is a trivial question. Unfortunately I don't have my copy of V&R S Programming to hand. Thanks for your help, Angelo -- ------------------------------------------------------------------ | Angelo J. Canty Email: cantya at mcmaster.ca | | Mathematics and Statistics Phone: (905) 525-9140 x 27079 | | McMaster University Fax : (905) 522-0935 | | 1280 Main St. W. | | Hamilton ON L8S 4K1 |
The following seems to give what you want. tmp <- rep(w, each=nrow1*ncol1+nrow2*ncol2) * unlist(matlist) tmp <- rowSums(matrix(tmp,nr=nrow1*ncol1+nrow2*ncol2)) mat1 <- tmp[1:(nrow1*ncol1)]; dim(mat1) <- c(nrow1,ncol1) mat2 <- tmp[nrow1*ncol1+1:(nrow2*ncol2)]; dim(mat2) <- c(nrow2,ncol2) Giovanni -- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (479) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________]> Date: Fri, 31 Oct 2003 14:16:31 -0500 > From: Angelo Canty <canty at math.mcmaster.ca> > Sender: r-help-bounces at stat.math.ethz.ch > Organization: McMaster University > Precedence: list > > Hi, > > Suppose that I have a list where each component is a list of two > matrices. I also have a vector of weights. How can I collapse my > list of lists into a single list of two matrices where each matrix > in the result is the weighted sum of the corresponding matrices. > > I could use a loop but this is a nested calculation so I was hoping > there is a more efficient way to do this. To help clarify, here is > the code I would use with a for loop > > result <- list(mat1=matrix(0,nrow1,ncol1), > mat2=matrix(0,nrow2,ncol2)) > for (i in seq(along=matlist)) { > result$mat1 <- result$mat1+w[i]*matlist[[i]]$mat1 > result$mat2 <- result$mat2+w[i]*matlist[[i]]$mat2 > } > > I apologise if this is a trivial question. Unfortunately I don't have > my copy of V&R S Programming to hand. > > Thanks for your help, > Angelo > -- > ------------------------------------------------------------------ > | Angelo J. Canty Email: cantya at mcmaster.ca | > | Mathematics and Statistics Phone: (905) 525-9140 x 27079 | > | McMaster University Fax : (905) 522-0935 | > | 1280 Main St. W. | > | Hamilton ON L8S 4K1 | > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > >
Angelo Canty wrote:> Hi, > > Suppose that I have a list where each component is a list of two > matrices. I also have a vector of weights. How can I collapse my > list of lists into a single list of two matrices where each matrix > in the result is the weighted sum of the corresponding matrices. > > I could use a loop but this is a nested calculation so I was hoping > there is a more efficient way to do this. To help clarify, here is > the code I would use with a for loop > > result <- list(mat1=matrix(0,nrow1,ncol1), > mat2=matrix(0,nrow2,ncol2)) > for (i in seq(along=matlist)) { > result$mat1 <- result$mat1+w[i]*matlist[[i]]$mat1 > result$mat2 <- result$mat2+w[i]*matlist[[i]]$mat2 > } > > I apologise if this is a trivial question. Unfortunately I don't have > my copy of V&R S Programming to hand.Here is one possibility: result <- list( mat1 = matrix(rowSums(sapply(matlist, function(x)x$mat1) %*% diag(w)), nrow1, ncol1) mat2 = matrix(rowSums(sapply(matlist, function(x)x$mat2) %*% diag(w)), nrow2, ncol2) ) Warning: It doesn't have the readability that the original code has though. Paul. -- -------------------------------------------------------------------------- Dr. Paul Y. Peng, Associate Professor Phone: (709) 737 8080 Department of Mathematics and Statistics Fax: (709) 737 3010 Memorial University of Newfoundland E-mail: ypeng at math.mun.ca St. John's, NL A1C 5S7, Canada Web: www.math.mun.ca/~ypeng