Moving sum with variable window

8 次查看(过去 30 天)
The moving sum can be calculated in MATLAB with movsum. However, that function requires that the window be a scalar. Is there a faster way to calculate a moving sum when the window length varies without using a loop?
As it stands, I am working with two equally sized vectors. The first contains values to sum and the second contains window lengths. A for-loop is used to manually extract the required values from the first vector for each ith value (window length) in the second vector. It gets the job done, but it would be great if it could be optimized, as it is being run millions of times.
The window sizes in the second vector span a large enough range (e.g., 50) that it is not faster to pre-calculate moving sums for each window size and simply index into the corresponding pre-calculated vectors while looping.
If this question can be answered, I would also apply it to other moving functions, such as movmax and movmin.
Edited addition in response to Steven Lord's comment:
Yes, that is what I am asking. (Though the vectors are on the order of 100,000 in length with windows up to 500 in size.) Typically, sums are not centered, though. So, it would include the current and previous elements.
The following example should help:
% behavior like movsum(x,[y(i)-1 0])
% - not possible as movsum requires y to be a constant
% declare variables
x = 1:20;
y = [1 2 3 4 2 5 3 7 2 4 5 4 7 6 3 9 5 5 3 6];
s = zeros(1,20); % initialize vector for sums
% hand-calculated values
% s = [1 1+2 1+2+3 1+2+3+4 4+5 2+3+4+5+6 5+6+7 2+3+4+5+6+7+8 ...
% 8+9 7+8+9+10 7+8+9+10+11 9+10+11+12 7+8+9+10+11+12+13 ...
% 9+10+11+12+13+14 13+14+15 8+9+10+11+12+13+14+15+16 ...
% 13+14+15+16+17 14+15+16+17+18 17+18+19 15+16+17+18+19+20]
% s = [1 3 6 10 9 20 18 35 17 34 45 42 70 69 42 108 75 80 54 105]
% current loop method (would like to speed up this calculation)
for i = 1:20
s(i) = sum(x((i-y(i)+1):i));
end
disp(s)
1 3 6 10 9 20 18 35 17 34 45 42 70 69 42 108 75 80 54 105
% pre-compiled method described above
w_max = max(y);
s_arr = NaN(w_max,20);
for i = 1:w_max
s_arr(i,:) = movsum(x,[i-1 0]);
end
s = zeros(1,20); % initialize vector for sums
for i = 1:20
s(i) = s_arr(y(i),i);
end
disp(s)
1 3 6 10 9 20 18 35 17 34 45 42 70 69 42 108 75 80 54 105
  4 个评论
goc3
goc3 2022-1-21
Steven, I responded to your comment by updating my question with an added example.
Image Analyst, the window size changes as it scans. See the specific example that I added above.
apiwat nonut
apiwat nonut 2023-10-3
Hi, now I found the problam as same as you so, if you have any solution please tell me too.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2023-10-3
x = 1:20;
y = [1 2 3 4 2 5 3 7 2 4 5 4 7 6 3 9 5 5 3 6];
n=numel(x);
c=[0,cumsum(x)];
s=c(2:end)-c((2:n+1)-y)
s = 1×20
1 3 6 10 9 20 18 35 17 34 45 42 70 69 42 108 75 80 54 105
  1 个评论
goc3
goc3 2023-10-3
This is great! It is most definitely faster. Thanks, Matt.
For anyone that may use this answer, I was able to optimize it a bit further by adding one line that removes an indexing step:
x = 1:20;
y = [1 2 3 4 2 5 3 7 2 4 5 4 7 6 3 9 5 5 3 6];
n = numel(x);
cx = cumsum(x);
c = [0, cx];
s = cx - c((2:n+1) - y);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by