MATLAB vector correlation calculation

MATLAB vector type:

  • Row vector

  • Column vector

MATLAB line vector:

Creates a collection of elements enclosed in square brackets by a row vector, elements separated by spaces or commas.

r = [7 8 9 10 11]

Execute the above statement and return the following results:

r =       7              8              9             10        

MATLAB column vector:

Creates a collection of elements with column vectors enclosed in square brackets, separated by semicolons.

c = [7;  8;  9;  10; 11]

Execute the above statement and return the following results:

c =       7              8              9             10             11  

Reference vector element

One or more of several ways in which vector elements can be referenced. The component of a vector v is called v (i).

For example:

v = [ 1; 2; 3; 4; 5; 6];  % creating a column vector of 6 elementsv(3)

Execute the above statement and return the following results:

ans =     3 

When referring to a colon, a vector, for example, v (:), all components on the carrier are listed.

For example:

v = [ 1; 2; 3; 4; 5; 6];  % creating a column vector of 6 elementsv(:)

Execute the above statement and return the following results:

ans =     1     2     3     4     5     6

MATLAB allows you to select an element that ranges from a vector.

In the following example, we create a row vector RV with 9 elements. We will reference elements 3 to 7 to write RV (3:7) and create a new vector named sub_rv.

rv = [1 2 3 4 5 6 7 8 9];sub_rv = rv(3:7)

MATLAB will execute the above statement and return the following results:

sub_rv =     3     4     5     6     7

MATLAB vector dot product

The dot product a = (a1, a2,..., an) and b = (b1, b2,..., bn) of two vectors in MATLAB is given by:

a.b = ∑(ai.bi)

The dot product of two vectors a and b can be calculated by the following function:

dot(a, b);

Detailed examples

Create a script file in MATLAB with the following code:

v1 = [2 3 4];v2 = [1 2 3];dp = dot(v1, v2);disp('Dot Product:'); disp(dp);

Run the file and the results are as follows:

Dot Product:    20

Module of MATLAB vector

The amplitude of the elements v1, v2, v3,..., vn in vector v is given by the following formula:

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

MATLAB needs to calculate the modulus of vector according to the following steps:

  1. The vector taken and its product are multiplied by an array (*). This produces a vector sv whose elements are the sum of the squares of the elements of the vector, V

    sv = v.*v;

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

    dp= sum(sv);

  3. The square root of the sum obtained using the sqrt function, which is also the size V of the vector

    mag = sqrt(s);

Detailed examples

Create a script file in MATLAB with the following code:

v = [1: 2: 20];sv = v.* v;     %the vector with elements                 % as square of v's elementsdp = sum(sv);    % sum of squares -- the dot productmag = sqrt(dp);  % magnitudedisp('Magnitude:'); disp(mag);

Run the file and the results are as follows:

Magnitude:   36.4692

MATLAB append vector

MATLAB allows you to add vectors to the original vectors and jointly create new vectors.

If there are two row vectors r1 and r2, each of which has n and m elements, now create row vector r and put both n and m elements in row vector R. by attaching these vectors, write:

r = [r1,r2]

By adding these two vectors, the of vector r2, a matrix R can also be established. The second line of the matrix is written as follows:

r = [r1;r2]

It should be noted that to complete the above operation, the number of elements in the above two carriers should be the same.

Of course, the number of elements of N and m of the two column vectors c1 and c2 can be appended. To create a column vector c, put n plus m elements into it, and write by attaching these vectors:

c = [c1; c2]

You can also create a matrix c to append these two vectors; The vector c2 will the matrix of the second column:

c = [c1, c2]

It should also be noted that after completing the above operation, the number of elements in the above two carriers should be the same.

Detailed examples

Create a script file in MATLAB 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]

Run the file and the results are as follows:

r =     1     2     3     4     5     6     7     8rMat =     1     2     3     4     5     6     7     8c =     1     2     3     4     5     6     7     8cMat =     1     5     2     6     3     7     4     8

MATLAB transpose vector

Transpose operation in MATLAB can change a row vector into a column vector, and vice versa.

Transpose operation in MATLAB is represented by a single quotation mark (').

Detailed examples

Create a script file in MATLAB and enter the following code:

r = [ 1 2 3 4 ];tr = r';v = [1;2;3;4];tv = v';disp(tr); disp(tv);

Run the file and the results are as follows:

 1     2     3     4
     1     2     3     4

MATLAB scalar vector multiplication

MATLAB scalar multiplication: multiply a number by a vector.

Scalar multiplication produces a new vector of the same type, multiplying each element of the original vector by the number.

Detailed examples

Create a script file in MATLAB, and the code is as follows:

​​​​​​​

v = [ 12 34 10 8];m = 5 * v

Running the file produces the following results:

​​​​​​​

m =    60   170    50    40

You can also perform all scalar vector operations. For example, you can add, subtract, and split scalar vectors.

MATLAB vector addition and subtraction

In MATLAB, when adding and subtracting two vectors, the elements of the two vectors must have the same type and quantity.

Detailed examples

Create a script file in MATLAB 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);

Running the file produces the following results:

9    16    28    39    295     6     2     7   -11

MATLAB isometric element vector

When there are too many elements in a vector and the elements of the vector have the law of equal deviation, the direct input method will be too cumbersome. In this case, a colon (:) can be used to generate an isometric element vector.

How to build an equal difference element vector in MATLAB? The solution is as follows.

To establish the first element f of a vector v-band, the difference between the last element l and the element is any real number n, which can be written as follows:

v = [f : n : l]

Detailed examples

Create a script file in MATLAB with the following code:

v =[1: 2: 20];sqv = v.^2;disp(v);disp(sqv);

Run the file and the results are as follows:

1     3     5     7     9    11    13    15    17    191     9    25    49    81   121   169   225   289   361

Keywords: MATLAB

Added by xposed on Mon, 17 Jan 2022 18:05:15 +0200