Help with matrix manipulation

4 次查看(过去 30 天)
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

采纳的回答

John D'Errico
John D'Errico 2019-6-25
编辑:John D'Errico 2019-6-25
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.

更多回答(1 个)

Steven Lord
Steven Lord 2019-6-25
Since this seems like it might be a homework assignment, I'm simply going to point to one of the functions that may help you. Take a look at the help and/or the documentation for the cumsum function. That won't get you all the way there, but it will get you partway to the solution and show you other tools that will be useful for the whole solution.
  1 个评论
Noah Wilson
Noah Wilson 2019-6-25
Not a homework assignment, I am trying to figure this out for my internship and I have been stuck on it for a couple days. Thanks for the function suggestion though

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by