Monday, March 17, 2014

Vectors Examples

  • 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

    Create a script file with the following code:
    A = [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 
     
     
  • 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

    Create a script file with the following code:
    v = [ 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.
  • 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

    Create a script file with the following code:
    r = [ 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 
     
  • 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]
    When you run the file, it displays the following result:
    r =
         1     2     3     4     5     6     7     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

    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:
v = [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.4692  

  • Vector Dot Product

    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:
    v1 = [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 
     
  • 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

    Create a script file with the following code:
    v = [1: 2: 20];
    sqv = v.^2;
    disp(v);disp(sqv);
    When you run the file, it displays the following result:
    1     3     5     7     9    11    13    15    17    19
    1     9    25    49    81   121   169   225   289   361
    

No comments:

Post a Comment