MATLAB: How to add group of diagonal vectors in a matrix

MATLABmatrixsum

I have a 9800 * 49 matrix named "test1". I would like to sum 1:200 rows from column 1 and 201:400 rows from column 2 and so on… If I do it manually it would look like this:
A1=sum(test1(1:200,1));
A2=sum(test1(201:400,2));
A3=sum(test1(401:600,3));
A4=sum(test1(601:800, 4));
A5=sum(test1(801:1000, 5));
A6=sum(test1(1001:1200, 6));
….
A49=sum(test1(9601:9800, 49)
Is there any other way to do this other than manually?

Best Answer

Let A- your array.
A = randi(1e4,9800,49);
[m,n] = size(A);
k = 200;
A_out = sum(A(sub2ind([m,n],reshape(1:m,k,[]),ones(k,1)*(1:n))));
or
A_out = sum(A(bsxfun(@plus,1:m+k:m*n,(0:k-1)')));