How can I create overlapping subvectors?

For example I have a data set that has 12 elements as a vector.
x = [1;2;3;4;5;6;7;8;9;10;11;12]
I use reshape to create subvectors that split the data up as new vectors organized in an array.
array = reshape(x,4,[]);
Which gives me a way to get these vectors. So my new vectors are.
x1 = [1;2;3;4]
x2 = [5;6;7;8]
x3 = [9;10;11;12]
My question is there command that lets me get the new vectors as overlays or overlapping sections? Do I have to custom program this?
x1 = [1;2;3;4]
x2 = [3;4;5;6]
x3 = [5;6;7;8]
x4 = [7;8;9;10]
x5 = [9;10;11;12]

回答(2 个)

x = [1;2;3;4;5;6;7;8;9;10;11;12];
y = buffer(x, 4, 2);
>> y =
0 1 3 5 7 9
0 2 4 6 8 10
1 3 5 7 9 11
2 4 6 8 10 12
y = y(:, 2:end);
I assume, "x1, x2, ..." means "x(1, :), x(2, :), ...". Assigning different variables would be a bad idea.
out = x(bsxfun(@plus,(1:4).',0:2:numel(x)-3))
OR
out = x(cumsum([(1:4).',2*ones(4,(numel(x)-4)/2)],2))
OR
y=buffer(1:numel(x),4,2)
out = x(y(:,all(y)))
Use x1,x2... bad idea, please read here.

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by