MATLAB: Conditional sum on particular column of a matrix

conditionsum

Dear all, I am trying to sum first 5 elements of column 1 of matrix m x n one after the other. Eg. say 1 2 3 4 5 6 then I want result as 1 3 6 10 15 21. After than I want to repeat the process again for m rows. Please help me with this. Regards, Suri

Best Answer

You can use cumsum for this:
>> A = bsxfun(@plus,(1:6).',[0,2,5,10])
A =
1 3 6 11
2 4 7 12
3 5 8 13
4 6 9 14
5 7 10 15
6 8 11 16
>> cumsum(A,1)
ans =
1 3 6 11
3 7 13 23
6 12 21 36
10 18 30 50
15 25 40 65
21 33 51 81