将2d矩阵列表堆叠到3d矩阵的功能方法 [英] Functional way to stack list of 2d matrices into 3d matrix

查看:71
本文介绍了将2d矩阵列表堆叠到3d矩阵的功能方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

聪明的lapply之后,剩下了二维矩阵列表.

After a clever lapply, I'm left with a list of 2-dimensional matrices.

例如:

set.seed(1)
test <- replicate( 5,  matrix(runif(25),ncol=5), simplify=FALSE )
> test
[[1]]
          [,1]       [,2]      [,3]      [,4]      [,5]
[1,] 0.8357088 0.29589546 0.9994045 0.2862853 0.6973738
[2,] 0.2377494 0.14704832 0.0348748 0.7377974 0.6414624
[3,] 0.3539861 0.70399206 0.3383913 0.8340543 0.6439229
[4,] 0.8568854 0.10380669 0.9150638 0.3142708 0.9778534
[5,] 0.8537634 0.03372777 0.6172353 0.4925665 0.4147353

[[2]]
          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.1194048 0.9833502 0.9674695 0.6687715 0.1928159
[2,] 0.5260297 0.3883191 0.5150718 0.4189159 0.8967387
[3,] 0.2250734 0.2292448 0.1630703 0.3233450 0.3081196
[4,] 0.4864118 0.6232975 0.6219023 0.8352553 0.3633005
[5,] 0.3702148 0.1365402 0.9859542 0.1438170 0.7839465

[[3]]
...

我想将其转换为3维数组:

I'd like to turn that into a 3-dimensional array:

set.seed(1)
replicate( 5,  matrix(runif(25),ncol=5) )    

很明显,如果我正在使用复制,则只能打开simplify,但是sapply不能正确简化结果,并且stack完全失败. do.call(rbind,mylist)将其转换为2d矩阵,而不是3d阵列.

Obviously, if I'm using replicate I can just turn on simplify, but sapply does not simplify the result properly, and stack fails utterly. do.call(rbind,mylist) turns it into a 2d matrix rather than 3d array.

我可以循环执行此操作,但我正在寻找一种简洁实用的方法来处理它.

I can do this with a loop, but I'm looking for a neat and functional way to handle it.

我想出的最接近的方法是:

The closest way I've come up with is:

array( do.call( c, test ), dim=c(dim(test[[1]]),length(test)) )

但是我觉得这很不好(因为它会分解然后重新组装向量的数组属性,并且需要进行大量测试以确保安全性(例如,每个元素的尺寸相同).

But I feel like that's inelegant (because it disassembles and then reassembles the array attributes of the vectors, and needs a lot of testing to make safe (e.g. that the dimensions of each element are the same).

推荐答案

您可以使用abind程序包,然后使用abind(test, along = 3)

You can use the abind package and then use abind(test, along = 3)

library(abind)
testArray <- abind(test, along = 3)

或者您可以在对sapply的调用中使用simplify = 'array'(而不是lapply). simplify = 'array'simplify = TRUE不同,因为它将更改simplify2array

Or you could use simplify = 'array' in a call to sapply, (instead of lapply). simplify = 'array' is not the same as simplify = TRUE, as it will change the argument higher in simplify2array

例如

foo <- function(x) matrix(1:10, ncol = 5)
# the default is simplify = TRUE
sapply(1:5, foo)
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    1    1    1    1
 [2,]    2    2    2    2    2
 [3,]    3    3    3    3    3
 [4,]    4    4    4    4    4
 [5,]    5    5    5    5    5
 [6,]    6    6    6    6    6
 [7,]    7    7    7    7    7
 [8,]    8    8    8    8    8
 [9,]    9    9    9    9    9
[10,]   10   10   10   10   10
# which is *not* what you want
# so set `simplify = 'array'
sapply(1:5, foo, simplify = 'array')
, , 1

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

, , 3

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

, , 4

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

, , 5

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

这篇关于将2d矩阵列表堆叠到3d矩阵的功能方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆