After reading the MATLAB vector topic, you will able to implement row vectors and column vectors in MATLAB, you will understand vector types, theory, examples, and vector handling built-in Functions.
A MATLAB vector is a one-dimensional array of numbers. Square brackets are used for defining Vector in MATLAB. The semi-colon is used within the square brackets for separating the rows, and space or comma used within the square brackets for separating the elements.
MATLAB – Vectors types
In MATLAB we can create vector either in the row or in the column and we can say MATLAB allow us to type vectors in two types
- Row Vectors
- Column Vectors
MATLAB – Row Vectors
An array having only one row and any numbers of columns.
General Form:
x = [a1 a2 a3]
or
x = [a1, a2, a3]
The Row vectors x is created by typing elements a1, a2 and a3 within the square brackets and use comma or space for separating the elements.
Example
Aim (1): To create Row vector x having elements 2,3 and 5.
Program (1):
x=[2 3 5]
Output (1):
x = 2 3 5
or
Program (1):
x=[2,3,5]
Output (1):
x = 2 3 5
MATLAB – Column Vectors
An array having only one column and any numbers of rows.
General Form:
x = [a1; a2; a3]
The column vector x is created by typing elements a1, a2 and a3 within the square brackets and use the semicolon for separating the rows.
Example
Aim (1): To create Column vector x having elements 2,3 and 5.
Program (1):
x=[2;3;5]
Output (1)
x = 2 3 5
Selecting a single Element of a Vector
General Form:
c = x(i)
The variable c stores the element at position ith of vector x.
Example
Aim (1): To create row vector x having elements as shown below and also extract the element present at the 2nd position of row vector x.
The elements of vector x are 1, 30 and 20.
Program (1):
x=[1,30,20] c=x(2)
Output (1)
x = 1 30 20 c = 30
Selecting all Elements of a Vector
Using colon: operator, the whole vector elements can be accessed.
General Form:
c = x(:)
The variable c stores all the elements of vector x.
Example
Aim (1): To create row vector x having elements as shown below and also extract all the element in row vector x.
Program (1):
x=[1,30,20] c=x(:)
Output (1)
x = 1 30 20 c = 1 30 20
Explanation
- statement c=x(:) access all the elements of a row vector.
Selecting the range of Elements of a Vector
Consider the statements in MATLAB is given by
Output
x = 2 3 4 5 6 ans = 2 3 4Changing Elements of a Vector
Consider the statements in MATLAB is given by
Output
x = 2 3 4 x = 5 3 4Adding element to a Vector
Elements can be added to an existing vector.
output
x = 2 3 4 x = 2 3 4 5Appending Vector to a Vector
Two or more vectors can be joined and results in a single vector.
output
x = 2 3 y = 21 32 z = 2 3 21 32Deleting element of a Vector
Elements can be removed from an existing vector.
output
x = 2 3 4 x = 2 4Deleting all Elements of a Vector
Using colon: operator, the whole vector elements can be deleted.
Example
Aim (1): To create row vector x having elements as shown below and also extract all the element in row vector x.
Program (1):
x=[1,30,20] x(:)=[]Output (1)
x = []Explanation
- statement x(:)=[] removes all the elements of a row vector.
Deleting the range of Elements of a Vector
Consider the statements in MATLAB is given by
Output
x = 2 3 4 5 6 x = 5 6Vector handling built-in Functions
Some of commonly used vector handling functions like length(), size(), sort(), min() and max().
length() : This function gives the total number of items in a vector.
Consider the statements in MATLAB is given by
Output
x = 2 3 4 5 6 c = 5size() : This function gives total numbers of rows and columns in a vector.
Output
x = 2 3 4 5 6 c = 1 5sort() : This function arranges all the elements of a vector in ascending order.
Output
x = 22 13 14 52 6 c = 6 13 14 22 52max() : This function gives the largest item in the vector.
Output
12min() : This function gives the smallest item in the vector.
Output
3