简体   繁体   English

从R中的数组中的多个矩阵中提取并存储特定位置

[英]Extract and store a specific position from multiple matrices in an array in R

Sorry, newbie... I've got an array object called "y" of 500 matrices of 6x6, like this: 对不起,新手......我有一个名为“y”的数组对象,其中500个矩阵为6x6,如下所示:

, , 1
       [,1]   [,2]   [,3]   [,4]   [,5]   [,6]
[1,] 0.0000 0.3627 0.4132 0.4231 0.3795 0.5444
[2,] 0.3627 0.0000 0.2084 0.3523 0.2310 0.5377
[3,] 0.4132 0.2084 0.0000 0.1984 0.2920 0.4774
[4,] 0.4231 0.3523 0.1984 0.0000 0.2787 0.4363
[5,] 0.3795 0.2310 0.2920 0.2787 0.0000 0.5129
[6,] 0.5444 0.5377 0.4774 0.4363 0.5129 0.0000

[...]

, , 500
       [,1]   [,2]   [,3]   [,4]   [,5]   [,6]
[1,] 0.0000 0.3755 0.3568 0.3835 0.3582 0.5065
[2,] 0.3755 0.0000 0.0840 0.2253 0.2237 0.4066
[3,] 0.3568 0.0840 0.0000 0.1673 0.2434 0.4073
[4,] 0.3835 0.2253 0.1673 0.0000 0.2338 0.3403
[5,] 0.3582 0.2237 0.2434 0.2338 0.0000 0.4263
[6,] 0.5065 0.4066 0.4073 0.3403 0.4263 0.0000

I want to extract a specific position through all the 500 matrices in the array and store this 500 values in a vector named "unouno" for further analyses 我想通过数组中的所有500个矩阵提取特定位置,并将这500个值存储在名为“unouno”的向量中,以便进一步分析

I'm trying to do this: 我正在尝试这样做:

for (i in 1:dim(y)[[3]]){
unouno<-y[2,1,i, drop=F]
}

but it only extracts the value for the last (500th) matrix. 但它只提取最后一个(第500个)矩阵的值。

(Once solved this I want to extract and store separately the 500 values of each of the 6 x 6 positions in the matrices) (一旦解决了这个问题,我想分别提取和存储矩阵中每个6 x 6位置的500个值)

We can do this by leaving the 3rd dimension blank 我们可以将第三维留空

y[2,1,]

data 数据

y <- array(1:60, dim=c(10,2,3))

If you would like to fix your loop, this could be one way to do it: 如果你想修复你的循环,这可能是一种方法:

unouno <- NULL
for (i in 1:dim(y)[3]){
  unouno[i]<-y[2,1,i]
}

It seems that you were mising indexing on the vector unouno as well 看起来你也正在对矢量unouno进行索引

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

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