How to convert matrix into array?

倖福魔咒の 提交于 2020-01-03 02:52:07

问题


I have a large data set and i want to convert it into array. But for instance lets have a simple example. I have following matrix

`\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrrr}
\hline
& 1 & 2 & 3 & 4 & 5 & 6 \\ 
\hline
1 &   1 &  11 &  21 &  31 &  41 &  51 \\ 
2 &   2 &  12 &  22 &  32 &  42 &  52 \\ 
3 &   3 &  13 &  23 &  33 &  43 &  53 \\ 
4 &   4 &  14 &  24 &  34 &  44 &  54 \\ 
5 &   5 &  15 &  25 &  35 &  45 &  55 \\ 
6 &   6 &  16 &  26 &  36 &  46 &  56 \\ 
7 &   7 &  17 &  27 &  37 &  47 &  57 \\ 
8 &   8 &  18 &  28 &  38 &  48 &  58 \\ 
9 &   9 &  19 &  29 &  39 &  49 &  59 \\ 
10 &  10 &  20 &  30 &  40 &  50 &  60 \\ 
\hline
\end{tabular}
\end{center}
\end{table}
`

What i need is to convert this matrix into array of dimension (5,6,2), in which first matrix contains first five rows of "x" and second matrix will contain last 5 rows. I have tried

dim(x)<-c(5,6,2)

which makes the matrix-1 with 1:30 consecutive numbers and then matrix-2 with 31:60 consecutive numbers. Which is not my requirement.

Thanks in advance.


回答1:


What you offered is a LaTeX representation. The same matrix in R would be

M <- matrix(1:60, ncol=6)  # a 10 x 6 matrix

Consider using abind from package "abind":

require(abind)
abind( M[1:5,], M[6:10, ], along=3 )
#-----------------
, , 1

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1   11   21   31   41   51
[2,]    2   12   22   32   42   52
[3,]    3   13   23   33   43   53
[4,]    4   14   24   34   44   54
[5,]    5   15   25   35   45   55

, , 2

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    6   16   26   36   46   56
[2,]    7   17   27   37   47   57
[3,]    8   18   28   38   48   58
[4,]    9   19   29   39   49   59
[5,]   10   20   30   40   50   60

If you didn't want to use an outside package and would settle for the transposed version of each slice, then transpose first and then re-dimension. You cannot transpose an array, however, there is an aperm function you could look at:

Mt <- t(M)
dim(Mt) <- c(6, 5, 2)
Mt
, , 1

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]   11   12   13   14   15
[3,]   21   22   23   24   25
[4,]   31   32   33   34   35
[5,]   41   42   43   44   45
[6,]   51   52   53   54   55

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]    6    7    8    9   10
[2,]   16   17   18   19   20
[3,]   26   27   28   29   30
[4,]   36   37   38   39   40
[5,]   46   47   48   49   50
[6,]   56   57   58   59   60

The aperm solution:

> aperm(Mt, c(2,1,3))
, , 1

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1   11   21   31   41   51
[2,]    2   12   22   32   42   52
[3,]    3   13   23   33   43   53
[4,]    4   14   24   34   44   54
[5,]    5   15   25   35   45   55

, , 2

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    6   16   26   36   46   56
[2,]    7   17   27   37   47   57
[3,]    8   18   28   38   48   58
[4,]    9   19   29   39   49   59
[5,]   10   20   30   40   50   60



回答2:


How about:

M <- matrix(1:60,nrow=10)

M2 <- lapply(split(M,0:9%/%5),matrix,nrow=5)
M2
$`0`
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1   11   21   31   41   51
[2,]    2   12   22   32   42   52
[3,]    3   13   23   33   43   53
[4,]    4   14   24   34   44   54
[5,]    5   15   25   35   45   55

$`1`
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    6   16   26   36   46   56
[2,]    7   17   27   37   47   57
[3,]    8   18   28   38   48   58
[4,]    9   19   29   39   49   59
[5,]   10   20   30   40   50   60

And if you really need an array:

array(unlist(M2),c(5,6,2))



回答3:


I can't tell whether your sample data is really in that XML-ish form, or it just looks that way :-) . So long as you currently have a 2-dimensional matrix full of data in R, Chase's answer will probably do. If you want to be very careful, try

data.cube <- array(NA, dim = c(5,6,2))  
data.cube[,,1] <- your.matrix[1:5,]  
data.cube[,,2] <- your.matrix[6:10,]   

That's pedantic but I hope it'll point out some of the handy ways to feed data to an array in R.



来源:https://stackoverflow.com/questions/9572656/how-to-convert-matrix-into-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!