R中基于向量的多重矩阵生成

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

我在r中有一个(5x4)矩阵,即data定义如下:

data <- matrix(rnorm(5*4,mean=0,sd=1), 5, 4) 

我想创建4个遵循此公式的不同矩阵:假设data [code> data [ ,1] = [A1,A2,A3,A4,A5]。我想创建以下矩阵:

        A1*A1 A1*A2 A1*A3 A1*A4 A1*A5 
        A2*A1 A2*A2 A2*A3 A2*A4 A2*A5
   G1 = A3*A1 A3*A2 A3*A3 A3*A4 A3*A5
        A4*A1 A4*A2 A4*A3 A4*A4 A4*A5
        A5*A1 A5*A2 A5*A3 A5*A4 A5*A5

类似地,对于我想立即计算的其他列(g1g2g3) ,g4)。我该如何实现?

I have an (5x4) matrix in R, namely data defined as follows:

data <- matrix(rnorm(5*4,mean=0,sd=1), 5, 4) 

and I want to create 4 different matrices that follows this formula: Assume that data[,1] = [A1,A2,A3,A4,A5]. I want to create the following matrix:

        A1*A1 A1*A2 A1*A3 A1*A4 A1*A5 
        A2*A1 A2*A2 A2*A3 A2*A4 A2*A5
   G1 = A3*A1 A3*A2 A3*A3 A3*A4 A3*A5
        A4*A1 A4*A2 A4*A3 A4*A4 A4*A5
        A5*A1 A5*A2 A5*A3 A5*A4 A5*A5

Similarly for the other columns i want to calculate at once all the G matrices (G1,G2,G3,G4). How can i achieve that?

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

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

发布评论

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

评论(1

又爬满兰若 2025-01-27 00:39:48
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))

results

[[1]]
         [,1]     [,2]     [,3]      [,4]      [,5]
[1,]  0.37164  0.37582  0.33424 -0.105387  0.120936
[2,]  0.37582  0.38006  0.33800 -0.106574  0.122298
[3,]  0.33424  0.33800  0.30060 -0.094780  0.108765
[4,] -0.10539 -0.10657 -0.09478  0.029885 -0.034294
[5,]  0.12094  0.12230  0.10876 -0.034294  0.039354

[[2]]
         [,1]      [,2]     [,3]     [,4]      [,5]
[1,]  0.94684  0.117862 -1.01368  2.01456  0.719629
[2,]  0.11786  0.014671 -0.12618  0.25077  0.089579
[3,] -1.01368 -0.126183  1.08525 -2.15679 -0.770432
[4,]  2.01456  0.250772 -2.15679  4.28633  1.531132
[5,]  0.71963  0.089579 -0.77043  1.53113  0.546941

[[3]]
         [,1]      [,2]      [,3]     [,4]     [,5]
[1,]  1.61048  0.344159 -0.453466  2.68019 -0.57121
[2,]  0.34416  0.073547 -0.096906  0.57276 -0.12207
[3,] -0.45347 -0.096906  0.127684 -0.75467  0.16084
[4,]  2.68019  0.572758 -0.754669  4.46044 -0.95062
[5,] -0.57121 -0.122068  0.160837 -0.95062  0.20260

[[4]]
          [,1]      [,2]      [,3]       [,4]      [,5]
[1,]  0.559341  0.859297  0.451096 -0.0522063 -1.027929
[2,]  0.859297  1.320109  0.693004 -0.0802028 -1.579172
[3,]  0.451096  0.693004  0.363799 -0.0421032 -0.829002
[4,] -0.052206 -0.080203 -0.042103  0.0048727  0.095942
[5,] -1.027929 -1.579172 -0.829002  0.0959421  1.889075
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))

results

[[1]]
         [,1]     [,2]     [,3]      [,4]      [,5]
[1,]  0.37164  0.37582  0.33424 -0.105387  0.120936
[2,]  0.37582  0.38006  0.33800 -0.106574  0.122298
[3,]  0.33424  0.33800  0.30060 -0.094780  0.108765
[4,] -0.10539 -0.10657 -0.09478  0.029885 -0.034294
[5,]  0.12094  0.12230  0.10876 -0.034294  0.039354

[[2]]
         [,1]      [,2]     [,3]     [,4]      [,5]
[1,]  0.94684  0.117862 -1.01368  2.01456  0.719629
[2,]  0.11786  0.014671 -0.12618  0.25077  0.089579
[3,] -1.01368 -0.126183  1.08525 -2.15679 -0.770432
[4,]  2.01456  0.250772 -2.15679  4.28633  1.531132
[5,]  0.71963  0.089579 -0.77043  1.53113  0.546941

[[3]]
         [,1]      [,2]      [,3]     [,4]     [,5]
[1,]  1.61048  0.344159 -0.453466  2.68019 -0.57121
[2,]  0.34416  0.073547 -0.096906  0.57276 -0.12207
[3,] -0.45347 -0.096906  0.127684 -0.75467  0.16084
[4,]  2.68019  0.572758 -0.754669  4.46044 -0.95062
[5,] -0.57121 -0.122068  0.160837 -0.95062  0.20260

[[4]]
          [,1]      [,2]      [,3]       [,4]      [,5]
[1,]  0.559341  0.859297  0.451096 -0.0522063 -1.027929
[2,]  0.859297  1.320109  0.693004 -0.0802028 -1.579172
[3,]  0.451096  0.693004  0.363799 -0.0421032 -0.829002
[4,] -0.052206 -0.080203 -0.042103  0.0048727  0.095942
[5,] -1.027929 -1.579172 -0.829002  0.0959421  1.889075
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文