Problem 59055. Moving Movsum

Given a vector (x) and a window (y), calculate the sum s(k) corresponding to k-y(k)+1:k values of x
%%Example 1
x = [1 2 3 4 5 6];
y = [1 2 3 3 4 2];
%s(5) = sum(x(5-y(5)+1:5)) = sum(x(2:5))
s = [1, 1+2, 1+2+3, 2+3+4, 2+3+4+5, 5+6]
s = [1 3 6 9 14 11];
%%Example 2
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
y = [1 2 3 4 2 5 3 7 2 4 5 4 7 6 3 9 5 5 3 6];
% 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]
Only vectorized solutions will be accepted. Check the test suite for banned functions.

Solution Stats

100.0% Correct | 0.0% Incorrect
Last Solution submitted on Oct 26, 2025

Solution Comments

Show comments

Problem Recent Solvers3

Suggested Problems

More from this Author43

Community Treasure Hunt

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

Start Hunting!