# MATLAB: Help with matrix manipulation

loopMATLABmatrixmatrix manipulation

I have the following in matrix form and I am trying to figure out how to the following:
-Create column H
-Then enter the following into column H
+For H1 sum(1, B:G)
+For H2 sum(1:2, B:G)
+For H3 sum(1:3, B:G)
+Following this trend until I reach row 24
A B C D E F G H
1: 0.05 0 0 0 0 0 0
2: 0.15 1 1 0 4 0 0
3: 0.25 0 0 1 5 0 0
4: 0.35 0 0 0 1 0 0
5: 0.45 0 0 0 1 0 0
6: 0.55 0 0 1 1 0 0
7: 0.65 0 0 0 0 0 1
8: 0.75 0 0 1 1 0 0
9: 0.85 1 0 0 0 0 0
10: 0.95 2 0 0 0 1 0
11: 1.05 0 0 1 0 3 0
12: 1.15 1 0 0 2 1 0
13: 1.25 0 0 0 0 1 0
14: 1.35 1 0 0 0 1 0
15: 1.45 0 0 0 1 0 0
16: 1.55 0 0 0 0 0 0
17: 1.65 0 0 0 0 2 0
18: 1.75 0 0 0 0 0 0
19: 1.85 0 0 0 0 0 0
20: 1.95 0 0 0 0 0 0
21: 2.05 0 0 0 0 0 0
22: 2.15 0 0 0 0 2 0
23: 2.25 1 0 0 0 0 0
24: 2.35 0 0 0 0 0 0
I am unsure how to accomplish this in the most efficient way and would really appreciate the help. Thanks in advance

• I'm usually harder than Steve is on these things, but I am pretty sure it is not homework.
You want to form the sum of those elements, then stuff them into a new last column. So, what does this:
array(:,8) = sum(cumsum(array(:,2:7),1),2);
do? If you are not sure, then take it apart, starting at the inside. That is, assuming the matrix is called array (I'm not feeling very creative), what is this:
array(:,2:7)
Then look at this sub-expression:
cumsum(array(:,2:7),1)
Wrap one more operation around that:
sum(cumsum(array(:,2:7),1),2)
Then, stuff it into the 8'th column of array, expanding that array in the process, to get:
array(:,8) = sum(cumsum(array(:,2:7),1),2);
Anyway, learn MATLAB. Stop thinking in MATLAB as if you are using a spreadsheet. And, yes, that is what you are doing, because you are naming the columns of your arrays with letters. If you stay in that place, then you will always think of MATLAB as just another spreadsheet.