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일

0 개 추천

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