我有一个2D矩阵需要添加到3D矩阵中,例如:
mx3d <- array(1:60, c(3,4,5))
mx2d <- array(letters[1:15], c(3,5))
是否可以将此mx2d
添加到mx3d
中,以便mx3d.new
成为3x5x5
矩阵?如果我将矩阵转换为列表会更容易吗?谢谢!
为了说清楚,根据我们在mx2d
和mx3d
中的内容,我希望有类似的内容:
> mx3d.new
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 "a"
[2,] 2 5 8 11 "b"
[3,] 3 6 9 12 "c"
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 13 16 19 22 "d"
[2,] 14 17 20 23 "e"
[3,] 15 18 21 24 "f"
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 25 28 31 34 "g"
[2,] 26 29 32 35 "h"
[3,] 27 30 33 36 "i"
, , 4
[,1] [,2] [,3] [,4] [,5]
[1,] 37 40 43 46 "j"
[2,] 38 41 44 47 "k"
[3,] 39 42 45 48 "l"
, , 5
[,1] [,2] [,3] [,4] [,5]
[1,] 49 52 55 58 "m"
[2,] 50 53 56 59 "n"
[3,] 51 54 57 60 "o"
答案 0 :(得分:6)
library(abind)
mx3d.new <- abind(mx3d, mx2d, along= 2)
for(i in 1:5) print(mx3d.new[,,i])
这有用吗?
它给了我这个:
> for(i in 1:5) print(mx3d.new[,,i])
[,1] [,2] [,3] [,4] [,5]
[1,] "1" "4" "7" "10" "a"
[2,] "2" "5" "8" "11" "b"
[3,] "3" "6" "9" "12" "c"
[,1] [,2] [,3] [,4] [,5]
[1,] "13" "16" "19" "22" "d"
[2,] "14" "17" "20" "23" "e"
[3,] "15" "18" "21" "24" "f"
[,1] [,2] [,3] [,4] [,5]
[1,] "25" "28" "31" "34" "g"
[2,] "26" "29" "32" "35" "h"
[3,] "27" "30" "33" "36" "i"
[,1] [,2] [,3] [,4] [,5]
[1,] "37" "40" "43" "46" "j"
[2,] "38" "41" "44" "47" "k"
[3,] "39" "42" "45" "48" "l"
[,1] [,2] [,3] [,4] [,5]
[1,] "49" "52" "55" "58" "m"
[2,] "50" "53" "56" "59" "n"
[3,] "51" "54" "57" "60" "o"
答案 1 :(得分:2)
我不确定我是否理解你的问题,但
mx3d.new <- array(letters[1:75],c(3,5,5))
给你你想要的东西?或者如果你想重复一遍,
mx3d.new <- array(letters[1:15],c(3,5,5))
也许如果你给出一些你想要解决的问题的背景,我就能给予更多的帮助。
干杯, 吉姆