使用map2在R中将两个以上的矩阵列表相乘

发布于 2025-01-12 01:12:20 字数 649 浏览 2 评论 0原文

A = list(a = matrix(1:4, 2), b = matrix(2:5, 2))

G = list(a = matrix(10:13, 2), b = matrix(25:28, 2))

columns <- list(a = matrix(3:4, 1), b = matrix(1:2, 1))

想制作另一个列表中具有相同名称的所有元素的叉积列表,所以我这样做 purrr::map2(A, G, columns, `%*%`) 。看起来 map2 不允许超过两个元素。有解决方法吗?

Error in `stop_bad_length()`:
! Index 1 must have length 1, not 2
Backtrace:
 1. purrr::map2(A, G, columns, `%*%`)
 4. purrr:::stop_bad_element_length(...)
 5. purrr:::stop_bad_length(...)

我也尝试过 purrr::pmap(list(A, G, columns), %*%) ,但没有运气。

purrr::map2(A, G, `%*%`) 效果很好。

I have

A = list(a = matrix(1:4, 2), b = matrix(2:5, 2))

G = list(a = matrix(10:13, 2), b = matrix(25:28, 2))

columns <- list(a = matrix(3:4, 1), b = matrix(1:2, 1))

I want to make another list of cross-products of all the elements with the same name across lists, so I do purrr::map2(A, G, columns, `%*%`). It doesn't look like map2 allows more than two elements. Is there a workaround?

Error in `stop_bad_length()`:
! Index 1 must have length 1, not 2
Backtrace:
 1. purrr::map2(A, G, columns, `%*%`)
 4. purrr:::stop_bad_element_length(...)
 5. purrr:::stop_bad_length(...)

I also tried purrr::pmap(list(A, G, columns), %*%), but no luck.

purrr::map2(A, G, `%*%`) works beautifully.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2025-01-19 01:12:20

类似于 @Cettt 在 tidyverse 中的方法 - 我们在 pmap 中采用 list,然后使用 reduce >%*%。请注意,“列”元素需要转置

library(purrr)
pmap(list(A, G, columns), ~ reduce(list(..1, ..2, t(..3)), `%*%`))
$a
     [,1]
[1,]  333
[2,]  496

$b
     [,1]
[1,]  486
[2,]  647

或使用来自 base RMap 选项

Map(\(x, y, z) Reduce(`%*%`, list(x, y, t(z))), A, G, columns)
$a
     [,1]
[1,]  333
[2,]  496

$b
     [,1]
[1,]  486
[2,]  647

Similar to @Cettt's approach in tidyverse - where we take in list in pmap, and then reduce with %*%. Note that the the 'columns' elements needs to be transposed

library(purrr)
pmap(list(A, G, columns), ~ reduce(list(..1, ..2, t(..3)), `%*%`))
$a
     [,1]
[1,]  333
[2,]  496

$b
     [,1]
[1,]  486
[2,]  647

Or an option with Map from base R

Map(\(x, y, z) Reduce(`%*%`, list(x, y, t(z))), A, G, columns)
$a
     [,1]
[1,]  333
[2,]  496

$b
     [,1]
[1,]  486
[2,]  647
来日方长 2025-01-19 01:12:20

问题是您尝试应用的函数 ('%*%') 是一个只接受两个参数而不是三个参数的函数。

对于您的问题,我建议使用 Reduce (或 purrr::reduce):在这种情况下,Reduce 从两个矩阵开始并将它们相乘。然后将结果与第三个矩阵相乘。理论上你可以这样继续下去。

lapply(seq_along(A), \(k) Reduce('%*%', list(A[[k]],G[[k]], t(columns[[k]]))))

[[1]]
     [,1]
[1,]  333
[2,]  496

[[2]]
     [,1]
[1,]  486
[2,]  647

请注意,我必须转置 column 内的矩阵,因为它们的大小不正确。

The problem is that the function you are trying to apply ('%*%') is a function which only takes two arguments and not three.

For your problem I would recommend Reduce (or purrr::reduce): in this case Reduce starts with two matrices and multiplies them. Then it multiplies the result with the third matrix. Theoretically you could go on like this.

lapply(seq_along(A), \(k) Reduce('%*%', list(A[[k]],G[[k]], t(columns[[k]]))))

[[1]]
     [,1]
[1,]  333
[2,]  496

[[2]]
     [,1]
[1,]  486
[2,]  647

Note that I had to transpose the matrices inside column because they do not have the right size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文