简体   繁体   中英

apply functions by elements of two or more lists

Suppose I have 3 lists:

a=list(1,2,3)
b=list(matrix(c(1,2,3,4), ncol=2),matrix(c(3,2,3,5), ncol=2),matrix(c(1,4,2,2), ncol=2))
c=list(c(1,2),c(4,7),c(3,4))

and I want to create a vector based on operations by elements of the three lists, for example:

result=c(rep(NA,3))
for (j in 1:3) {
    result[j]=(a[[j]]-t(c[[j]])%*%b[[j]]%*%c[[j]])/2
}

what would be a more efficient way of doing this without using a loop?

Something like this?

mapply(function(x,y,z) (x-t(z)%*%y%*%z)/2, x = a, y = b, z = c)
[1]  -13.0 -215.5  -55.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM