如何制作这个矩阵的cumsum

时间:2012-07-17 06:13:59

标签: matlab matrix sum cumulative-sum

A= [ 1 2 4
     2 3 1
     3 1 2 ]

以便上面矩阵的答案应该是:

B = [ 1  3  7 
      9 12 13 
     16 17 19 ]

感谢

1 个答案:

答案 0 :(得分:5)

摆弄cumsumreshape可以帮助您:

B = reshape(cumsum(reshape(A', 1, [])), size(A))'
%# Equivalent to: B = A'; B = reshape(cumsum(B(:)), size(A))'

这会产生:

B =

     1     3     7
     9    12    13
    16    17    19