Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 矩阵中的累积和_Arrays_Performance_R_Matrix_Cumsum - Fatal编程技术网

Arrays 矩阵中的累积和

Arrays 矩阵中的累积和,arrays,performance,r,matrix,cumsum,Arrays,Performance,R,Matrix,Cumsum,我有一个矩阵 A= [ 1 2 4 2 3 1 3 1 2 ] 我想按行和列计算它的累积和,也就是说,我希望结果是 B = [ 1 3 7 3 8 13 6 12 19 ] 有没有关于如何在R中快速制作的想法?(可能使用函数cumsum) (我有巨大的矩阵) 谢谢 一行: t(apply(apply(A, 2, cumsum)), 1, cumsum)) 基本观察结果是,您可以首先计算列上的累积和,然后计算该矩阵在行上的累积和 注意

我有一个矩阵

A= [ 1 2 4
     2 3 1
     3 1 2 ]
我想按行和列计算它的累积和,也就是说,我希望结果是

B = [ 1  3  7 
      3  8  13 
      6  12 19 ]
有没有关于如何在R中快速制作的想法?(可能使用函数cumsum) (我有巨大的矩阵)

谢谢

一行:

t(apply(apply(A, 2, cumsum)), 1, cumsum))
基本观察结果是,您可以首先计算列上的累积和,然后计算该矩阵在行上的累积和

注意:在执行行时,必须转置生成的矩阵

你的例子是:

> apply(A, 2, cumsum)
     [,1] [,2] [,3]
[1,]    1    2    4
[2,]    3    5    5
[3,]    6    6    7

> t(apply(apply(A, 2, cumsum), 1, cumsum))
     [,1] [,2] [,3]
[1,]    1    3    7
[2,]    3    8   13
[3,]    6   12   19
关于性能:我现在知道这种方法在大型矩阵中的扩展有多好。就复杂性而言,这应该接近最优。通常,
apply
的性能也没有那么差


编辑 现在我开始好奇了——哪种方法更好?简短的基准:

> A <- matrix(runif(1000*1000, 1, 500), 1000)
> 
> system.time(
+   B <- t(apply(apply(A, 2, cumsum), 1, cumsum))
+ )
       User      System     elapsed 
      0.082       0.011       0.093 
> 
> system.time(
+   C <- lower.tri(diag(nrow(A)), diag = TRUE) %*% A %*% upper.tri(diag(ncol(A)), diag = TRUE)
+ )
       User      System     elapsed 
      1.519       0.016       1.530 
>A
>系统时间(
+B
>系统时间(

+C下面是一个使用matrixStats包和一个更大的示例矩阵的更高效的实现:

library(matrixStats)
A <- matrix(runif(10000*10000, 1, 500), 10000)

# Thilo's answer
system.time(B <- t(apply(apply(A, 2, cumsum), 1, cumsum)))
user  system elapsed 
3.684   0.504   4.201

# using matrixStats
system.time(C <- colCumsums(rowCumsums(A)))
user  system elapsed 
0.164   0.068   0.233 

all.equal(B, C)
[1] TRUE
库(matrixStats)

A我的解决方案:函数cumsum_row()(见下文)获取矩阵M并返回M行的累积和矩阵。函数cumsum_col()对列做同样的事情

cumsum_row <- function(M) {
  M2 <- c()
  for (i in 1:nrow(M))
    M2 <- rbind(M2, cumsum(M[i,]))
  return (M2)
}

cumsum_col <- function(M) {
  return (t(cumsum_row(t(M))))
}

cumsum_row+1我真的想不出比这更好的了(尽管删除的答案是这样说的-严重的大脑故障,再加上一个错误创建了
a
)。@Gavin:brain fail一直在发生-至少在我的情况下;)-然而,你的解决方案让我想到了。矩阵与三角矩阵相乘是可行的。在MATLAB中:
tril(ones(3,3))*A*triu(ones(3,3))
.R遗憾的是,它不能很好地支持三角矩阵,因此创建合适的矩阵可能会扼杀所有可以通过矩阵乘法存档的速度增益。不过这是个好主意。@Thilo是的,这正是我在大脑出现故障之前的想法,
diag()
潜入其中。@Thilo那将是
lower.tri(A,T)%*%A%*%upper.tri(A,T)
在R.@Charles Neat one中。我的大脑出现了故障-不久前,我就知道了这些功能…感谢您提供的这段代码片段,它可能会提供一些有限的、即时的帮助。通过说明这是解决问题的一个好方法,A将极大地提高其长期价值,并将使它对其他读者更有用r、 类似的问题。请编辑您的答案,添加一些解释,包括您所做的假设。
  > M <- matrix(rep(1, 9), nrow=3)
  > M
         [,1] [,2] [,3]
    [1,]    1    1    1
    [2,]    1    1    1
    [3,]    1    1    1

  > cumsum_row(M)
         [,1] [,2] [,3]
    [1,]    1    2    3
    [2,]    1    2    3
    [3,]    1    2    3