MATLAB: How to sum every 30 columns

columnreshapesum

I have a matrix with 31 rows and 930 columns, I want to sum every 30 columns of this matrix and finally get a 31*31matrix. Say, the first cloumn equals to the sum from 1st to 30th column, and the second equals to 31st adds to 60th, so on and so forth.
I run my code like below, but the result seems not correct:
out=squeeze(sum(reshape(A,31,30,[]),2));
Here, A is my matrix with a dimension of 31*930.
I am wondering how to solve it? Thanks

Best Answer

The final matrix should be 31*30
out=squeeze(sum(reshape(A,31,30,[]),3)); % sum should be along 3rd dimension