[R] multi

arun smartpink111 at yahoo.com
Tue Nov 5 19:40:13 CET 2013


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.




More information about the R-help mailing list