向量化矩阵

发布于 2024-10-09 17:17:21 字数 347 浏览 0 评论 0原文

我有一个 1000 x 1000 的大型二维矩阵。我想对其进行整形,使其成为一列(或行)。例如,如果矩阵是:

A B C
1 4 7
2 5 8
3 6 9

我想把它变成:

1 2 3 4 5 6 7 8 9

我不需要保留列标题,只需保留数据的顺序。如何使用 reshape2 (这是我认为最容易使用的包)来执行此操作?


为了澄清起见,我提到了reshape,因为我认为这是实现此目的的最佳方法。我可以看到有一些更简单的方法,我对此非常满意。

I have a large 2D matrix that is 1000 x 1000. I want to reshape this so that it is one column (or row). For example, if the matrix was:

A B C
1 4 7
2 5 8
3 6 9

I want to turn it in to:

1 2 3 4 5 6 7 8 9

I do not need to preserve the column headers, just the order of the data. How do I do this using reshape2 (which is the package that I presumed was the easiest to use)?


Just to clarify, I mentioned reshape as I thought it was the best way of doing this. I can see that there are simpler methods which I am perfectly happy with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

装纯掩盖桑 2024-10-16 17:17:21

我认为很难找到比以下更紧凑的方法:

c(m)
[1] 1 2 3 4 5 6 7 8 9

但是,如果您想保留矩阵结构,那么对 dim 属性的这种重新设计将是有效的:

dim(m) <- c(dim(m)[1]*dim(m)[2], 1)
m
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9

将会有更紧凑的方法来获取维度的乘积但上述方法强调了dim属性是矩阵的二元素向量。在该示例中获取“9”的其他方法包括:

> prod(dim(m))
[1] 9
> length(m)
[1] 9

I think it will be difficult to find a more compact method than:

c(m)
[1] 1 2 3 4 5 6 7 8 9

However, if you want to retain a matrix structure, then this reworking of the dim attribute would be be effective:

dim(m) <- c(dim(m)[1]*dim(m)[2], 1)
m
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9

There would be more compact methods of getting the product of the dimensions but the above method emphasizes that the dim attribute is a two element vector for matrices. Other ways of getting the "9" in that example include:

> prod(dim(m))
[1] 9
> length(m)
[1] 9
随心而道 2024-10-16 17:17:21

一个可能的解决方案,但不使用 reshape2:

> m <- matrix(c(1:9), ncol = 3)
> m
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> as.vector(m)
[1] 1 2 3 4 5 6 7 8 9

A possible solution, but without using reshape2:

> m <- matrix(c(1:9), ncol = 3)
> m
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> as.vector(m)
[1] 1 2 3 4 5 6 7 8 9
谈情不如逗狗 2024-10-16 17:17:21

来吧,R 伙计们,让我们给 OP 一个 reshape2 解决方案:

> m <- matrix(c(1:9), ncol = 3)
> melt(m)$value
[1] 1 2 3 4 5 6 7 8 9

我只是懒得去测试它比 c(m) 慢多少。不过,它是一样的:

> identical(c(m),melt(m)$value)
[1] TRUE

[编辑:哦,我在跟谁开玩笑:]

> system.time(for(i in 1:1000){z=melt(m)$value})
   user  system elapsed 
  1.653   0.004   1.662 
> system.time(for(i in 1:1000){z=c(m)})
   user  system elapsed 
  0.004   0.000   0.004 

Come on R guys, lets give the OP a reshape2 solution:

> m <- matrix(c(1:9), ncol = 3)
> melt(m)$value
[1] 1 2 3 4 5 6 7 8 9

I just cant be bothered to test how much slower it is than c(m). It is the same, though:

> identical(c(m),melt(m)$value)
[1] TRUE

[EDIT: oh heck who am I kidding:]

> system.time(for(i in 1:1000){z=melt(m)$value})
   user  system elapsed 
  1.653   0.004   1.662 
> system.time(for(i in 1:1000){z=c(m)})
   user  system elapsed 
  0.004   0.000   0.004 
清泪尽 2024-10-16 17:17:21

as.vector(m) 应该比 c(m) 更有效:

> library(rbenchmark)
> m <- diag(5000)
> benchmark(
+   vect = as.vector(m), 
+   conc = c(m), 
+   replications=100
+ )
  test replications elapsed relative user.self sys.self user.child sys.child
2 conc          100  12.699    1.177     6.952    5.754          0         0
1 vect          100  10.785    1.000     4.858    5.933          0         0

as.vector(m) should be little more efficient then c(m):

> library(rbenchmark)
> m <- diag(5000)
> benchmark(
+   vect = as.vector(m), 
+   conc = c(m), 
+   replications=100
+ )
  test replications elapsed relative user.self sys.self user.child sys.child
2 conc          100  12.699    1.177     6.952    5.754          0         0
1 vect          100  10.785    1.000     4.858    5.933          0         0
木落 2024-10-16 17:17:21

一种更简单的方法是使用函数“sapply”(或者也可以使用“for”循环来完成相同的操作)

 m <- matrix(c(1:9), ncol = 3)
 (m1 <- as.numeric(sapply(1:NROW(m), function(i)(m[,i]))))

One more simple way to do it by using function "sapply" (or the same could be done with 'for' loop as well)

 m <- matrix(c(1:9), ncol = 3)
 (m1 <- as.numeric(sapply(1:NROW(m), function(i)(m[,i]))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文