Help with matrix manipulation
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Noah Wilson
el 25 de Jun. de 2019
Editada: John D'Errico
el 25 de Jun. de 2019
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
0 comentarios
Respuesta aceptada
John D'Errico
el 25 de Jun. de 2019
Editada: John D'Errico
el 25 de Jun. de 2019
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.
0 comentarios
Más respuestas (1)
Steven Lord
el 25 de Jun. de 2019
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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!