MATLAB: Creating an array of sums of another array.

arrayarray manipulationloopsMATLABmatrix arraymatrix manipulationsumwhile loop

I have an array of 322,872 data points. I want to create another array of the sum of the numbers in the first array in segments of 730 data points (sum from 1 to 730 would be the first point in the new array, sum from 731 to 1,460 would be the second point in the new array and so on). The code I have right now (which is not working) is the following:
i = 1;
j = 730;
while j < 322871;
i = i + 730;
j = j + 730;
k = 1;
x = zeros(442,1);
while k < 441;
x(k,:) = sum(WS_50mCopy(i:j,:));
end
end

Best Answer

n = 18; %n = 322872 in your case
windowSize = 5;% n = 730 in your case
% generate vector of size n
x = round(rand(1,n) * 100)
lastWindowSize = mod(n, windowSize);
% reshape x to a matrix of the wanted window size
x1 = reshape([x zeros(1, windowSize - lastWindowSize)], windowSize, (n + windowSize - lastWindowSize)/windowSize)
% sum each column of the matrix to generate the sums vector
y = sum(x1, 1)
These are the vectors that were generated:
x =
60 47 70 70 64 3 7 32 53 65 41 82 72 97 53 33 11 61
x1 =
60 3 41 33
47 7 82 11
70 32 72 61
70 53 97 0
64 65 53 0
y =
311 160 345 105