Vectors


>> In this lecture we will discuss about the vectors in MATLAB and their operation. A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors:


1. Row vectors


2. Column vectors


1. Row vectors:


To create a row vector, separate the elements by commas or by space. For example,





2. Column vectors:


To create a column vector by using the transpose notation ( ' ). Or you can also create a column vector by separating the elements by semicolons. For example,




Referencing the Elements of a Vector:


You can reference one or more of the elements of a vector in several ways. The ith component of a vector v is referred as v(i). For example:




When you reference a vector with a colon, such as v(:), all the components of the vector are listed.




MATLAB allows you to select a range of elements from a vector.



For example, let us create a row vector rv of 9 elements, then we will reference the elements 3 to 7 by writing rv(3:7) and create a new vector named sub_rv.



>> You can create vectors by ''appending'' one vector to another. For example, to create the row vector u whose first three columns contain the values of r = [2, 4, 20] and whose fourth, fifth, and sixth columns contain the values of w = [9, -6, 3], you type u = [r, w]. The result is the vector u = [2, 4, 20, 9, -6, 3].


>> The colon operator (:) easily generates a large vector of regularly spaced elements.

Typing x = [m:q:n] creates a vector x of values with a spacing q. The first value is m. For example, typing x = [0: 2: 8] creates the vector x = [0, 2, 4, 6, 8], whereas typing x = [0: 2: 7] creates the vector x = [0, 2, 4, 6].

>> To create a row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = [5: 0.1: 8]. If the increment q is omitted, it is presumed to be 1. Thus typing y = [-3: 2] produces the vector y = [-3, -2, -1, 0, 1, 2].


>> The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment. The syntax is linspace(x1, x2, n), where x1 and x2 are the lower and upper limits and n is the number of points. For example, linspace(5,8,31) is equivalent to [5:0.1:8]. If n is omitted, the spacing is 1.


>> The logspace command creates an array of logarithmically spaced elements. Its syntax is logspace(a, b, n), where n is the number of points between 10a and 10b. For example, x = logspace(-1,1,4) produces the vector x = [0.1000, 0.4642, 2.1544, 10.000]. If n is omitted, the number of points defaults to 50.


Vector Operations:


let us discuss the following vector operations:


a. Addition and Subtraction of Vectors


b. Scalar Multiplication of Vectors


c. Transpose of a Vector


d. Appending Vectors


e. Magnitude of a Vector


f. Vector Dot Product


g. Vectors with Uniformly Spaced Elements



a. Addition and Subtraction of Vectors:


You can add or subtract two vectors. Both the operand vectors must be of same type and have same number of elements.



Example:



b. Scalar Multiplication of Vectors:


When you multiply a vector by a number, this is called the scalar multiplication. Scalar multiplication produces a new vector of same type with each element of the original vector multiplied by the number.



Example:



c. Transpose of a Vector:


The transpose operation changes a column vector into a row vector and vice versa. The transpose operation is represented by a single quote(').



Example:



d. Appending Vectors:


MATLAB allows you to append vectors together to create new vectors. If you have two row vectors r1 and r2 with n and m number of elements, to create a row vector r of n plus m elements, by appending these vectors, you write:


r = [r1, r2]


You can also create a matrix r by appending these two vectors, the vector r2, will be the second row of the matrix:


r = [r1; r2]


However, to do this, both the vectors should have same number of elements.


Similarly, you can append two column vectors c1 and c2 with n and m number of elements. To create a column vector c of n plus m elements, by appending these vectors, you write:


c = [c1; c2]


You can also create a matrix c by appending these two vectors; the vector c2 will be the second column of the matrix:


c = [c1, c2]


However, to do this, both the vectors should have same number of elements.



Example:

Create a script file with the following code:


r1 = [ 1 2 3 4 ];

r2 = [5 6 7 8 ];
r = [r1,r2]
rMat = [r1;r2]

c1 = [ 1; 2; 3; 4 ];
c2 = [5; 6; 7; 8 ];
c = [c1; c2]

cMat = [c1,c2]

you will get




e. Magnitude of a Vector:


Magnitude of a vector v with elements v1, v2, v3, …, vn, is given by the equation:


|v| = √ (v1^2 + v2^2 + v3^2 + … + vn^2)


You need to take the following steps to calculate the magnitude of a vector:


Take the product of the vector with itself, using array multiplication (.*). This produces a vector sv, whose elements are squares of the elements of vector v.


sv = v.*v;


Use the sum function to get the sum of squares of elements of vector v. This is also called the dot product of vector v.


dp= sum(sv);


Use the sqrt function to get the square root of the sum which is also the magnitude of the vector v.



mag = sqrt(s);

Example:




e. Magnitude of a Vector:


Dot product of two vectors a = (a1, a2, …, an) and b = (b1, b2, …, bn) is given by:


a.b = ∑(ai.bi)


Dot product of two vectors a and b is calculated using the dot function.


dot(a, b);



Example:



g. Vectors with Uniformly Spaced Elements:


MATLAB allows you to create a vector with uniformly spaced elements. To create a vector v with the first element f, last element l, and the difference between elements is any real number n, we write:


v = [f : n : l]



Example: