Suppose I have the inputs data = [1 2 3 4 5 6 7 8 9 10]
and num = 4
. I want to use these to generate 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?
To create an array with four elements in a single row, separate the elements with either a comma ( , ) or a space. This type of array is a row vector. To create a matrix that has multiple rows, separate the rows with semicolons. Another way to create a matrix is to use a function, such as ones , zeros , or rand .
In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers and using semicolons to mark the end of each row. In the same way, you can create a sub-matrix taking a sub-part of a matrix. In the same way, you can create a sub-matrix taking a sub-part of a matrix.
You can create a vector both by enclosing the elements in square brackets like v=[1 2 3 4 5] or using commas, like v=[1,2,3,4,5]. They mean the very same: a vector (matrix) of 1 row and 5 columns. It is up to you.
Here's one option using the function HANKEL:
>> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With