How to do sliding window operation in Matlab?

5 views (last 30 days)
Ram k
Ram k on 18 May 2016
Commented: Andrei Bobrov on 18 May 2016
Suppose I Have a sequence
x=[4,1,1,1,2,2,3,5,9,7,7,7,6,6,1,1,2,3,4,4],
and I want addition for sliding window of size 10, i.e. addition of sequence
[4,1,1,1,2,2,3,5,9,7]
i.e. 4+1+1+1+2+2+3+5+9+7=35,then skipping 1st and adding next element of sequence i.e. addition for
[1,1,1,2,2,3,5,9,7,7],
then next i.e. addition for
[1,1,2,2,3,5,9,7,7,7]
likewise upto last window i.e.
[7,7,6,6,1,1,2,3,4,4],
how to do it.

Answers (2)

Duncan Po
Duncan Po on 18 May 2016
Edited: Duncan Po on 18 May 2016
R2016a has a new function movsum:
>> x=[4,1,1,1,2,2,3,5,9,7,7,7,6,6,1,1,2,3,4,4]
>> movsum(x,10,'EndPoints','discard')
ans =
35 38 44 49 54 53 52 51 49 44 41
Loren's blog has an article on these new additions: http://blogs.mathworks.com/loren/2016/04/15/moving-along/

Andrei Bobrov
Andrei Bobrov on 18 May 2016
Edited: Andrei Bobrov on 18 May 2016
conv2(x,ones(1,10),'valid')
add
n = 10;
out = hankel(x(1:end-n+1),x(n+1:end));

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!