R 中所有列表对象的同时矩阵运算

发布于 2025-01-20 00:20:01 字数 934 浏览 0 评论 0原文

我在R中有一个矩阵列表,如下

set.seed(1234)
data <- matrix(rnorm(3*4,mean=0,sd=1), 3, 4) 
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))

所示:

[[1]]
           [,1]        [,2]       [,3]
[1,]  1.4570077 -0.33487534 -1.3089918
[2,] -0.3348753  0.07696698  0.3008557
[3,] -1.3089918  0.30085569  1.1760127

[[2]]
          [,1]       [,2]       [,3]
[1,]  5.502298 -1.0065968 -1.1870541
[2,] -1.006597  0.1841480  0.2171611
[3,] -1.187054  0.2171611  0.2560926

[[3]]
          [,1]      [,2]      [,3]
[1,] 0.3303260 0.3141712 0.3244131
[2,] 0.3141712 0.2988064 0.3085474
[3,] 0.3244131 0.3085474 0.3186061

[[4]]
          [,1]      [,2]      [,3]
[1,] 0.7921673 0.4247196 0.8886017
[2,] 0.4247196 0.2277129 0.4764227
[3,] 0.8886017 0.4764227 0.9967755

我希望每个列表对象总结列并找到这些求和的最小值。例如min.results [[1]] = min(-0.186,0.042,0.167)= -0.186

I have a list of matrices in R as follows,

set.seed(1234)
data <- matrix(rnorm(3*4,mean=0,sd=1), 3, 4) 
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))

which results in:

[[1]]
           [,1]        [,2]       [,3]
[1,]  1.4570077 -0.33487534 -1.3089918
[2,] -0.3348753  0.07696698  0.3008557
[3,] -1.3089918  0.30085569  1.1760127

[[2]]
          [,1]       [,2]       [,3]
[1,]  5.502298 -1.0065968 -1.1870541
[2,] -1.006597  0.1841480  0.2171611
[3,] -1.187054  0.2171611  0.2560926

[[3]]
          [,1]      [,2]      [,3]
[1,] 0.3303260 0.3141712 0.3244131
[2,] 0.3141712 0.2988064 0.3085474
[3,] 0.3244131 0.3085474 0.3186061

[[4]]
          [,1]      [,2]      [,3]
[1,] 0.7921673 0.4247196 0.8886017
[2,] 0.4247196 0.2277129 0.4764227
[3,] 0.8886017 0.4764227 0.9967755

I want for each list object to sum the columns and find the minimum of these summation. For example min.results[[1]] = min(-0.186,0.042,0.167)=-0.186.

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

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

发布评论

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

评论(1

就是爱搞怪 2025-01-27 00:20:01

我们可以使用 sapply 循环遍历 list,获取按列求和 (colSums) 并返回 min >imum

sapply(results, \(x) min(colSums(x)))

-output

[1] -0.1868594 -0.7138005  0.9215250  1.1288552

或使用 collapse

library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005  0.9215250  1.1288552

We may use sapply to loop over the list, get the column wise sum (colSums) and return with the minimum

sapply(results, \(x) min(colSums(x)))

-output

[1] -0.1868594 -0.7138005  0.9215250  1.1288552

Or using collapse

library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005  0.9215250  1.1288552
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文