简体   繁体   English

从数组列表中的每个数组中选择每个第n个矩阵

[英]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.

相关问题 使用np.zeros数组为原始矩阵的每个单独的行和列创建单个数组 - create individual arrays from np.zeros array using ones for each separate row and column of original matrix 从一个数组(包含嵌套数组)中选择 x 个随机元素,并且每个嵌套数组中不超过一个 - Select x random elements from an array (that contains nested arrays) and no more than one from each nested array 在Matlab中的每第n行之后,在矩阵中的每个序列中插入一行到另一个矩阵 - Inserting One Row Each Time in a Sequence from Matrix into Another Matrix After Every nth Row in Matlab 在Postgresql的数组数组中迭代每个元素 - Iterating on each element from an array of arrays on Postgresql 从应该包含每个数组的元素的数组列表中找出最短的序列长度 - Find the shortest sequence length out from list of arrays which should contain element from each array 在具有重复项的数组列表中计算每个不同的数组 - Counting each distinct array occurrence in a list of arrays with duplicates 如何构建一个 numpy 数组列表,其中每个数组的大小都是唯一的 - How to build a list of numpy arrays where each array is uniquely sized 从数组中选择与每个项目匹配的行 - Select rows that match each item from array 从总线阵列中的每个总线选择信号 - select signal from each bus in array of busses 有一个Java数组列表; 如何按每个数组的最后一个元素排序? - Have a List of Arrays in Java; How to Sort By the Last Element of Each Array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM