Creating an array of sums of another array.
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
TADA
2018-12-26
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!