Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate the following matrix and vector from the given input data in MATLAB?

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?

like image 826
Lazer Avatar asked Nov 04 '09 18:11

Lazer


People also ask

How do you create a matrix and a vector 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 .

How do we generate the matrix in MATLAB?

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.

How do you create a vector in MATLAB?

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.


Video Answer


1 Answers

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
like image 200
gnovice Avatar answered Oct 15 '22 10:10

gnovice