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

 Accepted Answer

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 Comments

Thank you for the answer, however, Iam getting the following error
"You cannot subscript a table using linear indexing (one subscript) or
multidimensional indexing (three or more subscripts). Use a row subscript and a
variable subscript."
Attach your table here as part of the mat file.
RelativeX = 2500 x 1 double
RelativeY = 2500 x 1 double
or
XY = 2500 x 2 double
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)');
works thank you

Sign in to comment.

More Answers (2)

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 Comments

Thank you for the answer, much helpful, and what if the data is arranged like (n x 2) array
x1 y1
x2 y2
x3 y3
x4 y4
x5 y5
. .
xn yn
and I want to get n number of (2 x 5) arrays
1st sequence (2 x 5) array
x1, x2, x3, x4, x5
y1, y2, y3, y4, y5
2nd sequence (2 x 5) array
x2, x3, x4, x5, x6
y2, y3, y4, y5, y6
3rd sequence (2 x 5) array
x3, x4, x5, x6, x7
y3, y4, y5, y6, y7
4rth sequence (2 x 5) array
x4, x5, x6, x7, x8
y4, y5, y6, y7, y8
..................... xn
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.
I have already done that. I need the data as separate sequences for running through various analyses. And I am looking into it.
"I need the data as separate sequences for running through various analyses."
Then you can easily use an ND array or a cell array, exactly as John D'Errico wrote.

Sign in to comment.

Dwarka Sahu
Dwarka Sahu on 25 Sep 2019
for i=1:5
sprintf('%g, %g, %g, %g, %g',i, i+1, i+2, i+3, i+4)
end

3 Comments

Thank you for the answer, much helpful, and what if the data is arranged like (n x 2) array
x1 y1
x2 y2
x3 y3
x4 y4
x5 y5
. .
xn yn
and I want to get n number of (2 x 5) arrays
1st sequence (2 x 5) array
x1, x2, x3, x4, x5
y1, y2, y3, y4, y5
2nd sequence (2 x 5) array
x2, x3, x4, x5, x6
y2, y3, y4, y5, y6
3rd sequence (2 x 5) array
x3, x4, x5, x6, x7
y3, y4, y5, y6, y7
4rth sequence (2 x 5) array
x4, x5, x6, x7, x8
y4, y5, y6, y7, y8
..................... xn
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
RelativeX = 2500 x 1 double
RelativeY = 2500 x 1 double
or
XY = 2500 x 2 double
Thank you for the answer, however, Iam getting the following
>> for i=1:5
sprintf('RelativeX%g, RelativeY%g', i, i)
end
for i=1:5
sprintf('RelativeX%g, RelativeX%g, RelativeX%g, RelativeX%g, RelativeX%g', i, i+1,i+2,i+3,i+4)
sprintf('RelativeY%g, RelativeY%g, RelativeY%g, RelativeY%g, RelativeY%g', i, i+1,i+2,i+3,i+4)
end
ans =
'RelativeX1, RelativeY1'
ans =
'RelativeX2, RelativeY2'
ans =
'RelativeX3, RelativeY3'
ans =
'RelativeX4, RelativeY4'
ans =
'RelativeX5, RelativeY5'
ans =
'RelativeX1, RelativeX2, RelativeX3, RelativeX4, RelativeX5'
ans =
'RelativeY1, RelativeY2, RelativeY3, RelativeY4, RelativeY5'
ans =
'RelativeX2, RelativeX3, RelativeX4, RelativeX5, RelativeX6'
ans =
'RelativeY2, RelativeY3, RelativeY4, RelativeY5, RelativeY6'
ans =
'RelativeX3, RelativeX4, RelativeX5, RelativeX6, RelativeX7'
ans =
'RelativeY3, RelativeY4, RelativeY5, RelativeY6, RelativeY7'
ans =
'RelativeX4, RelativeX5, RelativeX6, RelativeX7, RelativeX8'
ans =
'RelativeY4, RelativeY5, RelativeY6, RelativeY7, RelativeY8'
ans =
'RelativeX5, RelativeX6, RelativeX7, RelativeX8, RelativeX9'
ans =
'RelativeY5, RelativeY6, RelativeY7, RelativeY8, RelativeY9'

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!