[英]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. 现在,我要从列表的每个元素中提取第n个(例如第二个)完整矩阵,并将它们组合在一起形成一个新数组。 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. 我发现了sapply和lapply的几种解决方案,当列表的元素是向量时效果很好,但是将数组作为列表元素从中应该提取整个矩阵会增加额外的复杂度,对此我无法找出解决方案。
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. 因此,我需要一种方法为列表中的所有数组自动选择每个数组的第n个矩阵。 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: 如@GavinSimpson的建议,要获取数组,请执行以下操作:
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: 另一种方法是重新定义来自sapply
调用的返回值:
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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.