Simple Looping + Sum Question

1 view (last 30 days)
Run
Run on 28 Aug 2012
The data I have is a large 20000 X 15 matrix, however, I will provide a simpler one so someone can help me easier.
Using the Matrix:
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
This is a 12 X 6 matrix, ignore the spaces. What I need to do is ignore completely the first column, then sum up the numbers in every 3rd row and print out the result. For example, starting from the first row 2+3+4+5+6=20, the moving onto the fourth row 20+2+3+4+5+6=40, then the 7th row 40+2+3+4+5+6=60, finally the 10th row 60+2+3+4+5+6=80.
The program should then loop back starting from the second row, and sum up the numbers from the second, fifth, 8th, 11th. Finally, it should loop back into the 3rd row, and sum up the numbers from 3rd, 6th, 9th, 12th. At the end, it should print out 3 numbers, one of them being 80, and the other two sums.
My program doesn't have the same numbers 3rd every row like that, I just used this as an example. Thanks! and I hope I have been clear on what I need.

Answers (1)

Andrei Bobrov
Andrei Bobrov on 28 Aug 2012
Edited: Andrei Bobrov on 28 Aug 2012
out = sum(reshape([sum(X(:,2:end),2);zeros(mod(-size(X,1),3),1)],3,[]),2);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!