简体   繁体   English

将函数应用于列表中的元素组合

[英]Apply a function over combinations of elements in a list

I would like to iterate through pairwise combinations of elements in a list in R, and then execute a function using the combination as an input. 我想遍历R中列表中元素的成对组合,然后使用该组合作为输入执行函数。

In this minimal example I (1) define three matrices, (2) combine them into a list containing the matrices as elements, and then (3) would like to calculate the dot product of the combinations of the elements in the list (ie. matrix 1 vs. matrix 2 and matrix 2 vs. matrix 3). 在这个最小的示例中,我(1)定义了三个矩阵,(2)将它们组合成一个包含矩阵作为元素的列表,然后(3)要计算列表中元素组合的点积(即矩阵1对矩阵2,矩阵2对矩阵3)。

set.seed

m1 = as.matrix(replicate(2, rnorm(2)))
m2 = as.matrix(replicate(2, rnorm(2)))
m3 = as.matrix(replicate(2, rnorm(2)))

matrix.list = list(m1, m2, m3)

dot.prod = function(matrix.x, matrix.y){
    return(matrix.x %*% matrix.y)
}

So far I have the following to have all combinations of matrix.list as inputs for dot.prod() using a nested loop. 到目前为止,我使用嵌套循环将matrix.list的所有组合作为dot.prod()的输入。

for (i in 1:length(matrix.list)){

  for (j in 1:length(matrix.list)){

            print(dot.prod(matrix.list[[i]], matrix.list[[j]]))
  }
}

Is it possible to do this by using a combinatorial function in R (like combn())? 是否可以通过在R中使用组合函数(例如combn())来做到这一点? I would be very grateful for any suggestions. 如有任何建议,我将不胜感激。

Edit: the function itself doesn't matter - I want to know how to use combinations of elements from a list as inputs for any R function 编辑:函数本身并不重要-我想知道如何使用列表中的元素组合作为任何R函数的输入

Let's just do 我们做吧

oo <- outer(matrix.list, matrix.list, Vectorize(crossprod, SIMPLIFY = FALSE))

which gives you a matrix list. 这会给您一个矩阵列表。 Accessing the result is handy. 访问结果很方便。 oo[1,2] (actually get into the list oo[1,2][[1]] ) gives the cross product between matrix 1 and matrix 2. oo[1,2] (实际上进入列表oo[1,2][[1]] )给出矩阵1和矩阵2之间的叉积。

Note, matrix cross product is not %*% (but if you insist, use Vectorize("%*%", SIMPLIFY = FALSE) ). 注意,矩阵叉积不是%*% (但是,如果您坚持要使用Vectorize("%*%", SIMPLIFY = FALSE) )。 As the operation is not symmetric, oo[1,2] is different from oo[2,1] . 由于操作不对称,所以oo[1,2]oo[2,1]

See How to perform pairwise operation like `%in%` and set operations for a list of vectors for the original idea of this. 有关此操作的原始思想,请参见如何执行“%in%”之类的成对运算,并为向量列表设置运算


Thanks for your response. 感谢您的答复。 To clarify: I want to calculate the dot product, not the cross product, as I stated in my initial question. 澄清一下:我想计算点积,而不是我在最初的问题中所述的叉积。 However, the function itself doesn't matter - I want to know how to use combinations of elements from a list as inputs for any R function. 但是,函数本身并不重要-我想知道如何使用列表中的元素组合作为任何R函数的输入。

No idea of what you want. 不知道你想要什么。 Give you a few other options and pick up yourself. 给您其他选择,然后振作起来。 They are all different. 它们都是不同的。

This? 这个?

combn(matrix.list, 2, function (u) u[[1]] %*% u[[2]])

This? 这个?

mapply("%*%", matrix.list[-length(matrix.list)], matrix.list[-1], SIMPLIFY = FALSE)

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

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