如何将矩阵转换为数组? [英] How to convert matrix into array?

查看:751
本文介绍了如何将矩阵转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数据集,我想将其转换为数组。但是举个简单的例子。
我有以下矩阵

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}
`

我需要的是将该矩阵转换为维数(5,6,2)的数组,其中第一个矩阵包含 x的前五行,第二个矩阵将包含后五行。
我尝试过

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)

这使得矩阵1的连续数字为1:30,然后矩阵2的连续数字为31:60。这不是我的要求。

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

预先感谢。

推荐答案

您提供的是LaTeX表示。 R中的相同矩阵为

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

考虑使用软件包 abind中的 abind

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

如果您不想使用外部软件包,并且愿意为每个切片换位而感到满意,然后先转置,然后再重新定尺寸离子。您不能转置数组,但是,可以查看一个 aperm 函数:

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

精子解决方案:

> 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

这篇关于如何将矩阵转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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