Simple Looping + Sum Question

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.

回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

提问:

Run
2012-8-28

Community Treasure Hunt

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

Start Hunting!

Translated by