A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors −
- Row vectors
- Column vectors
Row Vectors
Row vectors are created by enclosing the set of elements in square brackets, using space or comma to delimit the elements.
Live Demor = [7 8 9 10 11]
MATLAB will execute the above statement and return the following result −
r = 7 8 9 10 11
Column Vectors
Column vectors are created by enclosing the set of elements in square brackets, using semicolon to delimit the elements.
Live Democ = [7; 8; 9; 10; 11]
MATLAB will execute the above statement and return the following result −
c = 7 8 9 10 11
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 −
Live Demov = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements v(3)
MATLAB will execute the above statement and return the following result −
ans = 3
When you reference a vector with a colon, such as v(:), all the components of the vector are listed.
Live Demov = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements v(:)
MATLAB will execute the above statement and return the following result −
ans = 1 2 3 4 5 6
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.
Live Demorv = [1 2 3 4 5 6 7 8 9]; sub_rv = rv(3:7)
MATLAB will execute the above statement and return the following result −
sub_rv = 3 4 5 6 7
Vector Operations
In this section, let us discuss the following vector operations −
You can add or subtract two vectors. Both the operand vectors must be of same type and have same number of elements.
Example
Create a script file with the following code −
Live DemoA = [7, 11, 15, 23, 9]; B = [2, 5, 13, 16, 20]; C = A + B; D = A - B; disp(C); disp(D);
When you run the file, it displays the following result −
9 16 28 39 29 5 6 2 7 -11
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
Create a script file with the following code −
Live Demov = [ 12 34 10 8]; m = 5 * v
When you run the file, it displays the following result −
m = 60 170 50 40
Please note that you can perform all scalar operations on vectors. For example, you can add, subtract and divide a vector with a scalar quantity.
The transpose operation changes a column vector into a row vector and vice versa. The transpose operation is represented by a single quote (').
Example
Create a script file with the following code −
Live Demor = [ 1 2 3 4 ]; tr = r'; v = [1;2;3;4]; tv = v'; disp(tr); disp(tv);
When you run the file, it displays the following result −
1 2 3 4 1 2 3 4
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 −
Live Demor1 = [ 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]
When you run the file, it displays the following result −
r = Columns 1 through 7: 1 2 3 4 5 6 7 Column 8: 8 rMat = 1 2 3 4 5 6 7 8 c = 1 2 3 4 5 6 7 8 cMat = 1 5 2 6 3 7 4 8
Magnitude of a vector v with elements v1, v2, v3, …, vn, is given by the equation −
|v| = √(v12 + v22 + v32 + … + vn2)
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
Create a script file with the following code −
Live Demov = [1: 2: 20]; sv = v.* v; %the vector with elements % as square of v's elements dp = sum(sv); % sum of squares -- the dot product mag = sqrt(dp); % magnitude disp('Magnitude:'); disp(mag);
When you run the file, it displays the following result −
Magnitude: 36.469
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
Create a script file with the following code −
Live Demov1 = [2 3 4]; v2 = [1 2 3]; dp = dot(v1, v2); disp('Dot Product:'); disp(dp);
When you run the file, it displays the following result −
Dot Product: 20
No comments:
Post a Comment