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개)
Jan
2012년 3월 13일
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.
Andrei Bobrov
2012년 3월 13일
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)))
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!