[[1]] [,1] [,2] [1,] 0.003632559 0.2190629 [2,] -2.090772847 0.2190629 [[2]] [,1] [,2] [1,] 0.004278991 0.04337005 [2,] 0.190723602 0.04337005 [[3]] [,1] [,2] [1,] 0.01237827 -0.01544811 [2,] 0.06452200 -0.01544811 g<-c(1,2,3) function(g,hessianList){ for(i in 1:3){ ft1<-g[2]*hessianList[[2]] ft2<-sum(ft1) ft2 } } can u please help me find out what is the problem with this code i want to multiply matrix 1-3 by a vector g and sum the result. thanks [[alternative HTML version deleted]]
Hi, May be this helps: set.seed(42) ?list1 <- lapply(1:3, function(i) matrix(rnorm(4),2,2)) g <- c(1,2,3) fun1 <- function(g,hessianList){ ? mat1 <- vector(mode="list",length=length(g)) ?? for(i in seq_along(g)){ ???????? mat1[[i]] <- g[i]*hessianList[[i]] ??? ????????????????? } Reduce(`+`,mat1) ? } #or fun2 <- function(g,hessianList){ lst1 <- lapply(seq_along(g),function(i) g[i]*hessianList[[i]]) Reduce(`+`,lst1) } identical(fun1(g,list1),fun2(g,list1)) #[1] TRUE A.K. On Tuesday, November 5, 2013 1:02 PM, IZHAK shabsogh <ishaqbaba at yahoo.com> wrote: [[1]] ???????????? [,1]????? [,2] [1,]? 0.003632559 0.2190629 [2,] -2.090772847 0.2190629 [[2]] ??????????? [,1]?????? [,2] [1,] 0.004278991 0.04337005 [2,] 0.190723602 0.04337005 [[3]] ?????????? [,1]??????? [,2] [1,] 0.01237827 -0.01544811 [2,] 0.06452200 -0.01544811 g<-c(1,2,3) function(g,hessianList){ ?? for(i in 1:3){ ?? ??? ? ft1<-g[2]*hessianList[[2]] ?? ??? ? ft2<-sum(ft1) ?? ??? ? ft2 ?? } } can u please help me find out what is the problem with this code i want to multiply matrix 1-3 by a vector g and sum the result. thanks ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.
It's easier for folks to help you if you make your data available by reproducible code. You can do this using the dput() function. So, for example, you could share your hessianList with us using dput(hessianList) and then including this code in your post hessianList <- list(structure(c(0.003632559, -2.090772847, 0.2190629, 0.2190629 ), .Dim = c(2L, 2L)), structure(c(0.004278991, 0.190723602, 0.04337005, 0.04337005), .Dim = c(2L, 2L)), structure(c(0.01237827, 0.064522, -0.01544811, -0.01544811), .Dim = c(2L, 2L))) I'm not exactly sure what output you're looking for, but this may help ... # perform the calculation straight out sapply(seq(hessianList), function(i) sum(g[i]*hessianList[[i]])) # or # define a function to do the calculation on the provided vector and list myfun <- function(V, L){ sapply(seq(L), function(i) sum(V[i]*L[[i]])) } myfun(g, hessianList) Jean On Tue, Nov 5, 2013 at 11:40 AM, IZHAK shabsogh <ishaqbaba@yahoo.com> wrote:> > > [[1]] > [,1] [,2] > [1,] 0.003632559 0.2190629 > [2,] -2.090772847 0.2190629 > > [[2]] > [,1] [,2] > [1,] 0.004278991 0.04337005 > [2,] 0.190723602 0.04337005 > > [[3]] > [,1] [,2] > [1,] 0.01237827 -0.01544811 > [2,] 0.06452200 -0.01544811 > > g<-c(1,2,3) > > function(g,hessianList){ > for(i in 1:3){ > ft1<-g[2]*hessianList[[2]] > ft2<-sum(ft1) > ft2 > } > } > > can u please help me find out what is the problem with this code > > i want to multiply matrix 1-3 by a vector g and sum the result. > > > thanks > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org 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. > >[[alternative HTML version deleted]]