简体   繁体   English

通过两个或多个列表的元素应用函数

[英]apply functions by elements of two or more lists

Suppose I have 3 lists:假设我有 3 个列表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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