matlab: dividing vector into overlapping chunks of fixed size

Viewed 3923

I've a vector that I would like to split into overlapping subvectors of size cs in shifts of sh. Imagine the input vector is:

v=[1 2 3 4 5 6 7 8 9 10 11 12 13]; % A=[1:13]

given a chunksize of 4 (cs=4) and shift of 2 (sh=2), the result should look like:

[1 2 3 4]
[3 4 5 6]
[5 6 7 8]
[7 8 9 10]
[9 10 11 12]

note that the input vector is not necessarily divisible by the chunksize and therefore some subvectors are discarded. Is there any fast way to compute that, without the need of using e.g. a for loop? In a related post I found how to do that but when considering non-overlapping subvectors.

5 Answers
Related