Introduction to MATLAB vector

Vectors are one of the representations of arrays. It tends to be addressed in two different ways line(row) vector and section(column) vector. A vector is defined as enclosed arrangement of components. By utilizing square section we can create vectors in MATLAB.

MATLAB Vector

Defining a vector in MATLAB

MATLAB makes it simpler for you to enter matrices and vectors by providing software packages. A language, intended to look a great deal like the documentation use in direct variable is followed by an interface. We will examine a portion of the essentials of working with vectors in the following tutorial.

A vector is simply defined by a sequence of numeric numbers inside square braces as follows:

If we want to display the vector, typing label is enough to view:

If we increment other than one, we have to define the start number, increment value, and the last number.

 Let us type an example in command line, a vector that start with 4 and ends in 8 with steps of 0.25 .

Accessing elements within a vector

We can display individual elements in a vector. To access 2st element of a vector we use the following example, executed in MATLAB’s command line.

Note that in the previous example, the outcome isn’t shown if we end the line with a semi-colon. When we need to utilize MATLAB to work with enormous frameworks of conditions this will prove to be useful some other time.

MATLAB will permit you to see explicit pieces of the vector. In the event that you need to just gander at the initial three elements in a vector you can utilize a similar documentation to create the vector:

Let’s examine the above statement by executing the right commands to result the desired output.

Vector operations

MATLAB Vector

Addition and subtraction of vectors

We can subtract or add two vectors. The operand vectors involved in the operation must have same number of elements and also same data type to perform the operation.

Example: we have created two vectors A1 and B1, to perform the addition and subtraction operation, C and D variables are used to display the results of the operation using disp() function.

Output:

MATLAB Vector

Vectors scalar multiplication

Scalar multiplication:

Scalar multiplication means when we try to multiple a scalar let it be any integer with a vector.

Scalar multiplication produces a same type vector with each element multiplied by the number of original vector.

Example:

We can run this script file to obtain the output shown in the screenshot.

Output:

MATLAB Vector

Transpose of a vector

If you want to change a column vector( the vertical vector ) into a row vector(horizontal vector) and vice versa then transpose function can help.  This operation is denoted this symbol( ‘ ), which is called as single quote.

Example

We are creating a script file as follows –

Output:

MATLAB Vector

Appending vector

To append vectors together we use this append operation provided by MATLAB.

If we have vectors p1 and p2 with n and m number of elements, by appending these vectors, we get new vector  ?

Example:

We have created two vectors p1 and r2, to perform the operation, rMat and cmat variables are used to display the results of the operation.

p is a new combined vector of p1 and p2 vectors.

Output:

Magnitude of a vector

Magnitude of a vector p with elements p1, p2, p3, …,pn, is given by ?

Steps to calculate magnitude of a vector as follows:

  • A vector ps is produced by multiplying thevector product using (.*) by itself, whose elements are equal to squares of the corresponding elements of vector p as shown below.
  • ps = p.*p;
  • Now we perform dot product. We get the total value by addition of all elements of vector p by using the sum() function. This sum is obtained and stored in we.
  • we= sum(ps);
  • To get the magnitude of the vector p, we perform square root of values, use sqrt() function.
  • magnitude = sqrt(we);

Example:We are creating a script file as follows –the comments embedded in the file explains the performed operations on the matrices.

Output:

MATLAB Vector

Vector dot product

Dot product of two vectors p = (p1, p2, …,pn) and s = (s1, s2, …, sn) is given by ?

Example:

We are creating a script file as follows –

P1 and s1 are two vectors, by using dot() function we are calculating the dot product and therefore displayed the result using disp() function.

Output:

You can also refer the below screenshot for getting a clear understanding.

MATLAB Vector

Uniformly Spaced Elements vector

Uniformly spaced elements vector is created in MATLAB with the help of following syntax.

The above syntax shows us how to generate a vector p with the parameters:

First element = g

Last element=l

Difference between them=m (any real number)

Example: We are creating a script file as follows –

Output:

You can also refer the below screenshot for getting a clear understanding.

MATLAB Vector

Conclusion – Vectors in Matlab

In Matlab, with the help of vectors we can perform multiple logical operations like dot product of two vectors, subtraction of vectors, vector manipulation, finding square root of vector number, power square operation , addition scaling, normal vector multiplication, etc.

Pin It on Pinterest

Share This