简体   繁体   中英

Select each nth matrix from each array in a list of arrays

I have problems with selecting specific matrices in a list of arrays:

My dataset is a list. Each element of the list is an array, and each element of each array is naturally a matrix. Now what I want is to extract each nth (eg second) complete matrix from each element of the list, and combine them together into a new array. I have found several solutions with sapply and lapply, that work great when the elements of the list are vectors, but having arrays as list elements from which whole matrices should be extracted adds an additional level of complexity for which I cannot figure out a solution.

Here is an example:

#Create dataset    
set.seed(1000)
D1<-array(runif(16), dim=c(4, 2, 2))
D2<-array(runif(16), dim=c(4, 2, 2))
D3<-array(runif(16), dim=c(4, 2, 2))
Dat<-list(D1, D2, D3)#This is how my data look like

#This works and produces exactly what I want
array(c(Dat[[1]][,,2], Dat[[2]][,,2], Dat[[3]][,,2]), dim=c(4, 2, 3))

The last line of code manually produces exactly what I want. However, in the end the lists will of variable length and the arrays will have different lengths as well (although not the arrays contained in the same list, those will always be of equal length). Therefore I will need a way to pick each nth matrix per array for all arrays in the list automatically. I tried some things, but they all did not work:

  #This does not work at all
  array(Dat[[]][,,2], dim=c(4, 2, 3))
  array(Dat[[1:3]][,,2], dim=c(4, 2, 3))
  array(Dat[[c(1, 2, 3)]][,,2], dim=c(4, 2, 3))

  #This gives the the second row, first column of each matrix in the first array (i.e. first list element) only---absolutely not what I want
  sapply(Dat, "[[", 2)
  #Result
  [1] 0.7588465 0.6351620 0.9661789

Thanks in advance if anyone could help me out with that matter.

lapply( Dat , "[" , , , 2 )
#[[1]]
#          [,1]       [,2]
#[1,] 0.2157714 0.31708425
#[2,] 0.2561224 0.86581282
#[3,] 0.3499375 0.76416075
#[4,] 0.7554616 0.07288487
#
#...snip...

The above gives you a list of matrices.

To get the array, as suggested by @GavinSimpson , instead do:

simplify2array(lapply( Dat , "[" , , , 2 ))

(see edits for my earlier, less elegant suggestion for this)

Another way is to redimension the returned value from an sapply call:

temp <- sapply(Dat, "[", TRUE, TRUE, 2)
dim(temp) <- c(4,2,3)
temp
#-----------
, , 1

          [,1]       [,2]
[1,] 0.2157714 0.31708425
[2,] 0.2561224 0.86581282
[3,] 0.3499375 0.76416075
[4,] 0.7554616 0.07288487

, , 2

          [,1]       [,2]
[1,] 0.5483054 0.09076793
[2,] 0.6765533 0.12221227
[3,] 0.4518961 0.56751754
[4,] 0.8241250 0.04970992

, , 3

          [,1]      [,2]
[1,] 0.9962082 0.7979292
[2,] 0.5310467 0.7129898
[3,] 0.1099083 0.7028350
[4,] 0.6332116 0.5628327

You may try

library(abind)
ar1 <- abind(Dat, along=3)
ar1[,,seq(2,dim(ar1)[3], by=2)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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