有效地计算R中3d数组的行总和 [英] Efficiently compute the row sums of a 3d array in R

查看:113
本文介绍了有效地计算R中3d数组的行总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑数组a:

> a <- array(c(1:9, 1:9), c(3,3,2))
> a
, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

, , 2

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

我们如何有效地计算按三维索引的矩阵的行总和,使得结果为:

How do we efficiently compute the row sums of the matrices indexed by the third dimension, such that the result is:

     [,1] [,2]
[1,]   12   12
[2,]   15   15
[3,]   18   18

??

通过colSums()'dims'参数可以很容易地实现列总和:

The column sums are easy via the 'dims' argument of colSums():

> colSums(a, dims = 1)

但是我找不到在数组上使用rowSums()的方法来获得所需的结果,因为它对'dims'的解释与对colSums()的解释不同.

but I cannot find a way to use rowSums() on the array to achieve the desired result, as it has a different interpretation of 'dims' to that of colSums().

使用以下方法计算所需的行总和很简单:

It is simple to compute the desired row sums using:

> apply(a, 3, rowSums)
     [,1] [,2]
[1,]   12   12
[2,]   15   15
[3,]   18   18

但这只是隐藏循环.还有其他高效,真正矢量化的方法来计算所需的行总和吗?

but that is just hiding the loop. Are there other efficient, truly vectorised, ways of computing the required row sums?

推荐答案

@Fojtasek提到的拆分数组的答案让我想起了aperm()函数,该函数允许排列数组的维数.随着colSums()的工作,我们可以使用aperm()交换前两个维度,并在输出中运行colSums().

@Fojtasek's answer mentioned splitting up the array reminded me of the aperm() function which allows one to permute the dimensions of an array. As colSums() works, we can swap the first two dimensions using aperm() and run colSums() on the output.

> colSums(aperm(a, c(2,1,3)))
     [,1] [,2]
[1,]   12   12
[2,]   15   15
[3,]   18   18

此和其他建议的基于R的答案的一些比较时间:

Some comparison timings of this and the other suggested R-based answers:

> b <- array(c(1:250000, 1:250000),c(5000,5000,2))
> system.time(rs1 <- apply(b, 3, rowSums))
   user  system elapsed 
  1.831   0.394   2.232 
> system.time(rs2 <- rowSums3d(b))
   user  system elapsed 
  1.134   0.183   1.320 
> system.time(rs3 <- sapply(1:dim(b)[3], function(i) rowSums(b[,,i])))
   user  system elapsed 
  1.556   0.073   1.636
> system.time(rs4 <- colSums(aperm(b, c(2,1,3))))
   user  system elapsed 
  0.860   0.103   0.966 

因此在我的系统上,aperm()解决方案的显示速度略快:

So on my system the aperm() solution appears marginally faster:

> sessionInfo()
R version 2.12.1 Patched (2011-02-06 r54249)
Platform: x86_64-unknown-linux-gnu (64-bit)

但是,rowSums3d()给出的答案与其他解决方案不同:

However, rowSums3d() doesn't give the same answers as the other solutions:

> all.equal(rs1, rs2)
[1] "Mean relative difference: 0.01999992"
> all.equal(rs1, rs3)
[1] TRUE
> all.equal(rs1, rs4)
[1] TRUE

这篇关于有效地计算R中3d数组的行总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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