本文介绍了从R中的数组中提取并存储来自多个重采样的6×6矩阵的所有6×6柱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ,,1 
我有一个500,6X6矩阵的数组(y) [,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

我想通过数组中的所有500个矩阵提取每个矩阵位置(36),并存储500个值f每个位置在一个单独的向量(500个值的36个向量)。

到目前为止,在这里获得的帮助下,我可以提取500个特定位置的值(在这种情况下为1,1),并将它们存储在一个名为unouno的向量中,并使用下面的代码:

pre $ $ $ $ $ unono< -NULL
for(i in 1:dim(y)[[3]]){
unouno [i] }

我可以写这个代码36次,并提取所有的值,但我怎么能自动化呢?我应该使用嵌套循环(我已经试过他们)或者还有其他更好的方法吗?
Thanks!

解决方案

如果我们需要提取每个矩阵的位置,我们可以转置使用 aperm ,转换为矩阵,使用 nrow code>作为'y'的第三个维度。每列表示提取的位置元素。 (使用尺寸为6,6,2的可重复的例子)

pre $ matrix(aperm(y,c(3,1,2 ),nrow = dim(y)[3])
#[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
#[1,] 0 0.3627 0.4132 0.4231 0.3795 0.5444 0.3627 0 0.2084 0.3523 0.2310
#[2,] 0 0.3755 0.3568 0.3835 0.3582 0.5065 0.3755 0 0.0840 0.2253 0.2237
#[,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22 ]
#[1,] 0.5377 0.4132 0.2084 0 0.1984 0.2920 0.4774 0.4231 0.3523 0.1984 0
#[2,] 0.4066 0.3568 0.0840 0 0.1673 0.2434 0.4073 0.3835 0.2253 0.1673 0
#[,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32]
#[1,] 0.2787 0.4363 0.3795 0.2310 0.2920 0.2787 0 0.5129 0.5444 0.5377
#[2,] 0.2338 0.3403 0.3582 0.2237 0.2434 0.2338 0 0.4263 0.5065 0.4066
#[,33] [,34] [,35] [,36]
# [1,] 0.4774 0.4363 0.5129 0
#[2,] 0.4073 0.3403 0.4263 0
0,0.2084,0.3523,0.231,0.5377,0.4132,0.2084,0.01984,
0.292,0.4774,0.4231,0.3523,0.1984,0.0001, 0.2787,0.4363,0.3795,
0.231,0.229,0.2787,0,0.5129,0.5444,0.5377,0.4774,0.4363,
0.5129,0,0.3755,0.3568,0.3835,0.3582,0.5065,0.3755,
0,0.084,0.2253,0.2237,0.4066,0.3568,0.084,0,0.1673,0.2434,
0.4073,0.3835,0.2253,0.1673,0,0.2338,0.3403,0.3582,0.22237,
0.2434,0.2338,0,0.4263,0.5065,0.4066,0.4073,0.3403,0.4263,
0),.Dim = c(6L,6L,2L),.Dimnames = list(NULL,NULL,NULL))


I have an array (y) of 500, 6X6 matrices, like this:

, , 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 each matrix position (36) through all the 500 matrices in the array and store the 500 values of each position in a separate vector (36 vectors of 500 values).

Up till now, with the aid obtained here, I can extract the 500 values of a specific position (1,1 in this case) and store them in a vector called, say, "unouno", with this code:

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

I could write 36 times this code and extract all values, but how could I automate this? Should I use nested loops (I ve tried them unsuccesfully) or is there any other better way?Thanks!

解决方案

If we need to extract each matrix position, we can transpose the array using aperm, convert to matrix with nrow as the third dimension of 'y'. Each column represents the extracted elements for the position. (Using a reproducible example of dimensions 6,6,2)

matrix(aperm(y, c(3,1,2)), nrow=dim(y)[3])
#      [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7] [,8]   [,9]  [,10]  [,11]
#[1,]    0 0.3627 0.4132 0.4231 0.3795 0.5444 0.3627    0 0.2084 0.3523 0.2310
#[2,]    0 0.3755 0.3568 0.3835 0.3582 0.5065 0.3755    0 0.0840 0.2253 0.2237
#      [,12]  [,13]  [,14] [,15]  [,16]  [,17]  [,18]  [,19]  [,20]  [,21] [,22]
#[1,] 0.5377 0.4132 0.2084     0 0.1984 0.2920 0.4774 0.4231 0.3523 0.1984     0
#[2,] 0.4066 0.3568 0.0840     0 0.1673 0.2434 0.4073 0.3835 0.2253 0.1673     0
#     [,23]  [,24]  [,25]  [,26]  [,27]  [,28] [,29]  [,30]  [,31]  [,32]
#[1,] 0.2787 0.4363 0.3795 0.2310 0.2920 0.2787     0 0.5129 0.5444 0.5377
#[2,] 0.2338 0.3403 0.3582 0.2237 0.2434 0.2338     0 0.4263 0.5065 0.4066
#      [,33]  [,34]  [,35] [,36]
#[1,] 0.4774 0.4363 0.5129     0
#[2,] 0.4073 0.3403 0.4263     0

data

y <- structure(c(0, 0.3627, 0.4132, 0.4231, 0.3795, 0.5444, 0.3627, 
0, 0.2084, 0.3523, 0.231, 0.5377, 0.4132, 0.2084, 0, 0.1984, 
0.292, 0.4774, 0.4231, 0.3523, 0.1984, 0, 0.2787, 0.4363, 0.3795, 
0.231, 0.292, 0.2787, 0, 0.5129, 0.5444, 0.5377, 0.4774, 0.4363, 
0.5129, 0, 0, 0.3755, 0.3568, 0.3835, 0.3582, 0.5065, 0.3755, 
0, 0.084, 0.2253, 0.2237, 0.4066, 0.3568, 0.084, 0, 0.1673, 0.2434, 
0.4073, 0.3835, 0.2253, 0.1673, 0, 0.2338, 0.3403, 0.3582, 0.2237, 
0.2434, 0.2338, 0, 0.4263, 0.5065, 0.4066, 0.4073, 0.3403, 0.4263, 
0), .Dim = c(6L, 6L, 2L), .Dimnames = list(NULL, NULL, NULL))

这篇关于从R中的数组中提取并存储来自多个重采样的6×6矩阵的所有6×6柱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:19