Moving sum with variable window

24 views (last 30 days)
goc3
goc3 on 21 Jan 2022
Commented: goc3 on 3 Oct 2023
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 Comments
goc3
goc3 on 21 Jan 2022
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 on 3 Oct 2023
Hi, now I found the problam as same as you so, if you have any solution please tell me too.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 3 Oct 2023
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 Comment
goc3
goc3 on 3 Oct 2023
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);

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!