Generating sequences from data
古いコメントを表示
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
採用された回答
その他の回答 (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 件のコメント
Mahendran Subramanian
2019 年 9 月 25 日
John D'Errico
2019 年 9 月 25 日
First, DON'T make different arrays for each sequence. That is a programming style that will serve you terribly badly in the future. Instead, learn to use multidimensional arrays. Or cell arrays, etc.
As far as your problem goes, you just have TWO vectors, x and y. You already know how to solve the problem for a vector. (I just showed you in my answer.) So get theanswer for each vector of x and y, and then figure out how to combine them into ONE large array, in this case, it would be a 3-dimensional array.
Mahendran Subramanian
2019 年 9 月 26 日
Stephen23
2019 年 9 月 26 日
"I need the data as separate sequences for running through various analyses."
Dwarka Sahu
2019 年 9 月 25 日
0 投票
for i=1:5
sprintf('%g, %g, %g, %g, %g',i, i+1, i+2, i+3, i+4)
end
3 件のコメント
Mahendran Subramanian
2019 年 9 月 25 日
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
Mahendran Subramanian
2019 年 9 月 26 日
カテゴリ
ヘルプ センター および File Exchange で User-Defined Functions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!