Generating sequences from data
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi All,
I have a data set (x1, x2, x3, x4, x5, x6, x7, .... xn) from which I want to generate sequences like
x1, x2, x3, x4, x5
x2, x3, x4, x5, x6
x3, x4, x5, x6, x7
x4, x5, x6, x7, x8
..................... xn
Thank you
댓글 수: 0
채택된 답변
Andrei Bobrov
2019년 9월 26일
xy = [5 14
6 16
1 16
6 11
4 16
1 16
2 13];
n = 5;
[m,k] = size(xy);
out = xy(reshape(hankel(1:n,n:m),1,n,[]) + m*(0:k-1)')
댓글 수: 5
Andrei Bobrov
2019년 9월 26일
XY = readtable('XY.xlsx');
xy = XY{:,:};
n = 5;
[m,k] = size(xy);
out = xy(reshape(hankel(1:n,n:m),1,n,[]) + m*(0:k-1)');
추가 답변 (2개)
John D'Errico
2019년 9월 25일
So, given a vector x, of length n, you want to create the array with rows that are the sub-sequences of length 5? The result will be a (n-4) x 5 array.
A trivial solution would just concatenate columns to create the array. However, that would not be easily fixed if you then wnted to create sub-sequences of length 6 or 4.
So far better is to create an index array, then use that to index into the vector.
n = length(x);
m = 5;
ind = (1:n-m+1)' + (0:m-1);
A = x(ind);
This works for any length vector, and any size of sub-sequences.
It does use a feature of MATLAB that was introduced in R2016b, to create the index array ind. Earlier releases might use this instead:
ind = bsxfun(@plus,(1:n-m+1)',0:m-1);
댓글 수: 4
Stephen23
2019년 9월 26일
"I need the data as separate sequences for running through various analyses."
Dwarka Sahu
2019년 9월 25일
for i=1:5
sprintf('%g, %g, %g, %g, %g',i, i+1, i+2, i+3, i+4)
end
댓글 수: 3
Dwarka Sahu
2019년 9월 26일
For the first sequence of (nx2)
for i=1:5
sprintf('x%g, y%g', i, i)
end
For the next set of sequence of (2x5)
for i=1:5
sprintf('x%g, x%g, x%g, x%g, x%g', i, i+1,i+2,i+3,i+4)
sprintf('y%g, y%g, y%g, y%g, y%g', i, i+1,i+2,i+3,i+4)
end
참고 항목
카테고리
Help Center 및 File Exchange에서 User-Defined Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!