应用的功能的多维阵列,R VS MATLAB [英] Apply a function to a multi-dimensional array: R vs MATLAB

查看:160
本文介绍了应用的功能的多维阵列,R VS MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可以被视为与<一个href=\"http://stackoverflow.com/questions/18631165/arithmetic-mean-on-a-multidimensional-array-on-r-and-matlab-drastic-difference\">this 之一,这帮助我提高第r演出在计算一个大阵列上的平均值。不幸的是,在这种情况下,我尝试应用更复杂的东西(如位数计算)。

This question can be considered related to this one, that helped me to improve the R performances in computing the mean on a big array. Unfortunately, in this case I'm trying to apply something more complex (like a quantile calculation).

我已经有超过40百万元的4-D阵列和我想要计算在特定尺寸的第66百分位。这里有MATLAB的code:

I have a 4-D array with more than 40 millions of elements and I want to calculate the 66th percentile on a specific dimension. Here there is the MATLAB code:

> n = randn(100, 50, 100, 20);
> tic; q = quantile(n, 0.66, 4); toc
Elapsed time is 0.440824 seconds.

让我们做类似的东西R.

Let's do something similar in R.

> n = array(rnorm(100*50*100*20), dim = c(100,50,100,20))
> start = Sys.time(); q = apply(n, 1:3, quantile, .66); print(Sys.time() - start)
Time difference of 1.600693 mins

我意识到了MATLAB WRT R的更好的性能,但在这种情况下,我不知道该怎么办。也许我只需要等待2分钟,而不是一秒钟...
我希望有人能建议我任何的方式来提高运行时间,
无论如何,谢谢你提前...

I was aware of the better performances of MATLAB wrt R but in this case I don't know what to do. Probably I just need to wait 2 minutes instead of one second... I hope someone can suggest me any way to improve running times, anyway, thank you in advance...

更新
我申请了一些建议纳入的意见,我已经减少了运行时间:

UPDATE I've applied some of the suggestions into the comments and I've reduced the running time:

> start = Sys.time(); q = apply(n, 1:3, quantile, .66, names = FALSE); print(Sys.time() - start)
Time difference of 33.42773 secs

我们还远没有MATLAB的表演,但至少我已经学到了一些东西。

We're still far from the MATLAB performances but at least I've learnt something.

更新
我把这里的相关讨论'位数功能,<一个进步的一些href=\"http://stackoverflow.com/questions/26400517/operations-on-mult-dimensional-arrays-in-r-apply-vs-data-table-vs-plyr-paralle\">here.我上面显示相同code的运行时间从33传递到5秒...

UPDATE I put here some advancements related to `quantile' function discussed here. The running time of same code I've shown above has passed from 33 to 5 seconds...

推荐答案


RcppOctave
包调用
 的 GNU八度 的API函数;
如果你不已经知道的 GNU八度的,它的非常类似的 Matlab的的,旨在为完整compatiility。

The RcppOctave package calls the GNU Octave API functions; if you don't already know about GNU Octave, it is very similar to Matlab and aims for complete compatiility.

这是几乎一样快,Matlab的指导...

This is nearly as fast as Matlab direct...

library(RcppOctave)

set.seed(1)
n = array(rnorm(100*50*100*20), dim = c(100,50,100,20))

system.time( s <- octave_style_quantile(n, .66, 4) )
##    user  system elapsed 
##   0.526   0.048   0.574

# *R* `quantile` argument `type=5` is the method that matches matlab.
system.time( r <- apply(n, 1:3, quantile, .66, names = FALSE, type=5) )
##    user  system elapsed 
##  23.308   0.029  23.327

dim(r)
## [1] 100  50 100

identical(r,s)
## [1] TRUE


八度的一个相当简单的翻译
<一href=\"http://hg.savannah.gnu.org/hgweb/octave/file/38925538ec14/scripts/statistics/base/quantile.m\">quantile.m
至R

octave_style_quantile <- function (x, p=NULL, dim=NULL) {
  if ( is.null(p) ) p <- c(0.00, 0.25, 0.50, 0.75, 1.00)

  if ( is.null(dim) ) {
    ## Find the first non-singleton dimension.
    dim <- which(dim(x) > 1)[1];
  }

  stopifnot( is.numeric(x)||is.logical(x),
             is.numeric(p),
             dim <= length(dim(x)) )

  ## Set the permutation vector.
  perm <- seq_along(dim(x))
  perm[1] <- dim
  perm[dim] <- 1

  ## Permute dim to the 1st index.
  x <- aperm(x, perm);

  ## Save the size of the permuted x N-d array.
  sx = dim(x);

  ## Reshape to a 2-d array.
  dim(x) <- c( sx[1], prod(sx[-1]) );

  ## Calculate the quantiles.
  q = .CallOctave("quantile",x,p)

  ## Return the shape to the original N-d array.
  dim(q) <- c( length(p), sx[-1] )

  ## Permute the 1st index back to dim.
  q = aperm(q, perm);
  if( any(dim(q)==1) ) dim(q) <- dim(q)[-which(dim(q)==1)]
  q
}

这篇关于应用的功能的多维阵列,R VS MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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