Cumulative total of columns in a matrix or table

Just for fun, another approach using matrix-matrix multiplication:

acc = matrix1.UpperTriangularize[ConstantArray[1, {1, 1} Dimensions[matrix1][[2]]]];
acc // MatrixForm

$\left( \begin{array}{cccc} a & a+b & a+b+c & a+b+c+d \\ e & e+f & e+f+g & e+f+g+h \\ i & i+j & i+j+k & i+j+k+l \\ m & m+n & m+n+o & m+n+o+p \\ \end{array} \right)$

As alternative to Accumulate[matrix1], you could use

acc = LowerTriangularize[ConstantArray[1, {1, 1} Dimensions[matrix1[[1]]]].matrix1;
acc // MatrixForm

$\left( \begin{array}{cccc} a & b & c & d \\ a+e & b+f & c+g & d+h \\ a+e+i & b+f+j & c+g+k & d+h+l \\ a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \\ \end{array} \right)$

I would not advise to use these in practice as they have computational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.


I first came up with:

FoldList[Plus, #[[1]], Rest[#]] & /@ matrix

Then read the documentation for Accumulate, which says it is equivalent to:

Rest[FoldList[Plus, 0, #]] & /@ matrix1

One nice thing about these approaches is they generalize beyond sums:

FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
Rest[FoldList[Times, 1, #]] & /@ matrix1

Accumulate /@ matrix1

produces

{{a, a + b, a + b + c, a + b + c + d}, {e, e + f, e + f + g, 
  e + f + g + h}, {i, i + j, i + j + k, i + j + k + l}, {m, m + n, 
  m + n + o, m + n + o + p}}