How to create the following matrix and vector from given input in MATLAB?

Suppose I have inputs data = [1 2 3 4 5 6 7 8 9 10] and num = 4. I want to use them to create the following:

i = [1 2 3 4 5 6; 2 3 4 5 6 7; 3 4 5 6 7 8; 4 5 6 7 8 9]
o = [5 6 7 8 9 10]

which is based on the following logic:

length of data = 10
num = 4
10 - 4 = 6
i = [first 6; second 6;... num times]
o = [last 6]

What is the best way to automate this in MATLAB?

+5
source share
1 answer

Here, one of the options uses the HANKEL function :

>> data = 1:10;
>> num = 4;
>> i = hankel(data(1:num),data(num:end-1))

i =

     1     2     3     4     5     6
     2     3     4     5     6     7
     3     4     5     6     7     8
     4     5     6     7     8     9

>> o = i(end,:)+1

o =

     5     6     7     8     9    10
+10
source

All Articles