R中列表对象之间的矩阵计算

发布于 2025-01-20 00:58:05 字数 608 浏览 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]))

所有4个列表对象都dim = 3x3。我也有以下矩阵matr&lt; - 矩阵(c(2,4,6,8),ncol = 4),其中每个值对应于上面的列表对象。

然后,我使用此等式matr [,1]*matr [,2]*结果[[1]]*结果[[2]]在前两个对象之间以创建以下矩阵

          [,1]      [,2]       [,3]
[1,] 64.135122 2.6966755 12.4307531
[2,]  2.696676 0.1133865  0.5226732
[3,] 12.430753 0.5226732  2.4093448

如何计算所有所有可能对象组合的方程并将其保存到新列表中?

I have created list of objects 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]))

all 4 list objects have dim=3x3. I also have the following matrix matr <- matrix(c(2,4,6,8),ncol=4), where each value corresponds to the above list objects.

Then, I use this equation matr[,1]*matr[,2]*results[[1]]*results[[2]] between the first two objects in order to create the below matrix

          [,1]      [,2]       [,3]
[1,] 64.135122 2.6966755 12.4307531
[2,]  2.696676 0.1133865  0.5226732
[3,] 12.430753 0.5226732  2.4093448

How can I calculate the above equation for all all possible object combinations and save them to a new list?

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

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

发布评论

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

评论(1

末蓝 2025-01-27 00:58:05

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

> new_lst

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

1_2` [,1] [,2] [,3] [1,] 64.135122 2.6966755 12.4307531 [2,] 2.696676 0.1133865 0.5226732 [3,] 12.430753 0.5226732 2.4093448

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

1_3` [,1] [,2] [,3] [1,] 5.775451 -1.2624981 -5.095849 [2,] -1.262498 0.2759787 1.113939 [3,] -5.095849 1.1139391 4.496217

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

1_4` [,1] [,2] [,3] [1,] 18.46710 -2.275650 -18.610758 [2,] -2.27565 0.280422 2.293352 [3,] -18.61076 2.293352 18.755530

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

2_3` [,1] [,2] [,3] [1,] 43.621251 -7.589849 -9.242303 [2,] -7.589849 1.320590 1.608108 [3,] -9.242303 1.608108 1.958223

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

2_4` [,1] [,2] [,3] [1,] 139.47970 -13.680683 -33.754187 [2,] -13.68068 1.341852 3.310735 [3,] -33.75419 3.310735 8.168537

我们可以使用 combnlist 的序列上创建两两组合,提取元素并进行乘法

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-输出

3_4` [,1] [,2] [,3] [1,] 12.560327 6.404863 13.837154 [2,] 6.404863 3.266019 7.055953 [3,] 13.837154 7.055953 15.243778

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

> new_lst

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

1_2` [,1] [,2] [,3] [1,] 64.135122 2.6966755 12.4307531 [2,] 2.696676 0.1133865 0.5226732 [3,] 12.430753 0.5226732 2.4093448

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

1_3` [,1] [,2] [,3] [1,] 5.775451 -1.2624981 -5.095849 [2,] -1.262498 0.2759787 1.113939 [3,] -5.095849 1.1139391 4.496217

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

1_4` [,1] [,2] [,3] [1,] 18.46710 -2.275650 -18.610758 [2,] -2.27565 0.280422 2.293352 [3,] -18.61076 2.293352 18.755530

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

2_3` [,1] [,2] [,3] [1,] 43.621251 -7.589849 -9.242303 [2,] -7.589849 1.320590 1.608108 [3,] -9.242303 1.608108 1.958223

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

2_4` [,1] [,2] [,3] [1,] 139.47970 -13.680683 -33.754187 [2,] -13.68068 1.341852 3.310735 [3,] -33.75419 3.310735 8.168537

We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication

new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] * 
    results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")

-output

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