I have the following:
matrix1 = {{a, b, c, d}, {e, f, g, h}, {i, j, k, l}, {m, n, o, p}}
(Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)
I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n
, which is the column where the sum is to be placed. In other words, I want
matrix2 = {{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}}
I assume there must be a way to do this using Total[matrix1 ,{2}]
combined with some way to specify the number of columns to sum - maybe Range
...? Or should I be using Part
and Span
? If so, how?
Can anyone help with this? Thanks in advance.