2

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!

erselui
  • 35
  • 4

1 Answers1

1

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))
akrun
  • 874,273
  • 37
  • 540
  • 662