将矩阵转换为分配特定列的数组
Transform matrix to array assigning specific columns
我有一个大小为 17(行)x 6(列)的数字矩阵。它看起来像这样:
现在我想将此矩阵转换为大小为 2(行)x 3(列)x(17 维)的数组,每一行都转换为新数组中的一维,在某种程度上第 1-3 列转到第一行,第 4-6 列转到第二行。
我已使用示例中的数字举例说明维度 1 在这个新数组中的外观(它包括第一行的值):
如何将此矩阵转换为我想要的数组?
m <- matrix(c(1:12), ncol = 6)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 3 5 7 9 11
#[2,] 2 4 6 8 10 12
a <- array(t(m), dim = c(3, 2, length(m)/6))
a <- aperm(a, c(2, 1, 3)) #switch first and second dimension
#, , 1
#
# [,1] [,2] [,3]
#[1,] 1 3 5
#[2,] 7 9 11
#
#, , 2
#
# [,1] [,2] [,3]
#[1,] 2 4 6
#[2,] 8 10 12
我有一个大小为 17(行)x 6(列)的数字矩阵。它看起来像这样:
现在我想将此矩阵转换为大小为 2(行)x 3(列)x(17 维)的数组,每一行都转换为新数组中的一维,在某种程度上第 1-3 列转到第一行,第 4-6 列转到第二行。
我已使用示例中的数字举例说明维度 1 在这个新数组中的外观(它包括第一行的值):
如何将此矩阵转换为我想要的数组?
m <- matrix(c(1:12), ncol = 6)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 3 5 7 9 11
#[2,] 2 4 6 8 10 12
a <- array(t(m), dim = c(3, 2, length(m)/6))
a <- aperm(a, c(2, 1, 3)) #switch first and second dimension
#, , 1
#
# [,1] [,2] [,3]
#[1,] 1 3 5
#[2,] 7 9 11
#
#, , 2
#
# [,1] [,2] [,3]
#[1,] 2 4 6
#[2,] 8 10 12