OKPEDIA MATLAB EN VETTORI

How to make a vector in Matlab and Octave

To create a vector with Matlab or Octave type

vettore = [ a1; a2; a3; ... ; an ]

The elements of the vector are enclosed in square brackets.

Each element is separated from the next element in the list by a semicolon.

The result is a column vector.

Note. To create a line vector, separate elements with a comma or a space.

Examples

Example 1 (column vector)

To define this column vector

$$ v = \begin{pmatrix} 5 \\ 7 \\ 1 \\ 0 \\ -1 \end{pmatrix} $$

you have to create an array by separating the elements from each other with the semicolon symbol (;).

>> v = [5;7;1;0;-1]

In this way the elements of the vector are arranged in a single column divided into five different rows.

Example 2 (Row Vector)

To define a row vector

$$ v = \begin{pmatrix} 5 & 7 & 1 & 0 & -1 \end{pmatrix} $$

you have to create an array with the elements separated from each other by a comma (,)

>> v = [5,7,1,0,-1]

The same result is obtained by separating the elements with a space instead of a comma

>> v = [5 7 1 0 -1]

In both cases the vector has the elements arranged in a single row on five different columns.

Example 3

To create a vector of integers between a lower bound to an upper bound

>> v = [1:8]

The result is the vector

v = 1 2 3 4 5 6 7 8

Example 4

To create a vector of integers between a lower bound and an upper bound with a step other than +1, use the syntax [initial value: step: final value]

>> v = [10: -2 :1]

The result is

$$ v = \begin{pmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \end{pmatrix} $$

Example 5

The step can also be a decimal value.

>> v = [0: .2 :1]

The result is the vector

$$ v = \begin{pmatrix} 0.0 & 0.2 & 0.4 & 0.6 & 0.8 & 0.8 & 1.0 \end{pmatrix} $$

Example 6

To create a vector composed of n elements between a lower bound x1 and an upper bound x2 you can also use the function linspace(x1,x2,n)

>> v=linspace(0,1,5)

The result is the vector

$$ v = ( \ 0.00000 \ , \ 0.25000 \ , \ 0.50000 \ , \ 0.75000 \ , \ 1.00000 \ ) $$

Example 7

To create a null column vector use the function zeros(n,1)

>> zeros(5,1)

The output result is a null column vector

$$ v = \begin{pmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 0 \end{pmatrix} $$

Example 8

To create a null row vector, use the function zeros(1,n)

>> zeros(1,5)

The output result is a null row vector

$$ v = ( \ 0 \ , \ 0 \ , \ 0 \ , \ 0 \ , \ 0 \ ) $$

Example 9

To create a column vector with all elements equal to 1 use the function ones(n,1)

>> ones(5,1)

The output result is a column vector

$$ v = \begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} $$

Example 10

To create a row vector with all elements equal to 1 use the function ones(1,n)

>> ones(1,5)

The output result is a row vector

$$ v = ( \ 1 \ , \ 1 \ , \ 1 \ , \ 1 \ , \ 1 \ ) $$

https://how.okpedia.org/en/matlab/how-to-make-a-vector-in-matlab-and-octave


Report us an error or send a suggestion to improve this page


Arrays and Vectors


FacebookTwitterLinkedinLinkedin