## Introduction to Dot Product in MATLAB

The Dot Product of two vectors is defined as the projection that one vector has in the direction of the other. In simpler words, let ‘a’ be vector and ‘x’ be a unit vector, the dot product, given by an⋅x, is defined by the projection that ‘a’ will have in vector x’s direction. Please keep in mind that, in case of matrices, the dot product is defined only if the columns in the 1st matrix are equal in number to rows in the 2nd matrix. In MATLAB we use dot function to get the dot product of scalars or vectors.

**Syntax**

`A = dot (x, y)`

OR

`A = dot (x, y, dim)`

**Explanation: **

A = dot (x, y) is used to get the dot product of scalars, also referred to as the scalar dot product

To get the dot product of vectors ‘x’ and ‘y’, the vectors must be of the same length

A = dot (x, y, dim) will calculate the dot product along the dimension passed as an argument

### Examples to Implement Dot Product in MATLAB

Let us now understand the code of dot function in MATLAB using different examples:

#### Example #1

In this example, we will take 2 vectors and will compute their dot product using the dot function. For our first example, we will input the following values:

4.5 (7,013 ratings)

View Course

1. Pass the first input vector as [5 1 0 7 3]

2. Pass the second input vector as [1 5 -3 4 7]

**Code:**

```
x = [5 1 0 7 3]
y = [1 5 -3 4 7]
dot(x, y)
```

**Output:**

**Explanation: **First, Declaring the first input vector. Declaring the second input vector. Passing the input vectors to the dot function. * Mathematically, the dot product of vector [5 1 0 7 3] and [1 5 -3 4 7] is59. As we can see in the output, we have obtained the dot product of our input vectors as 59, which is the same as expected by us.

#### Example #2

In this example, we will take 2 complex vectors and will compute their dot product using the dot function. For this example, we will input the following values:

1. Pass the first complex vector as [5+2i 2+3i -4+i 3-6i 1+3i]

2. Pass the second complex vector as [5+i 4-2i 1+5i 2-3i 5-2i]

**Code:**

```
x = [5+2i 2+3i -4+i 3-6i 1+3i]
y = [5+i 4-2i 1+5i 2-3i 5-2i]
dot(x, y)
```

**Output:**

**Explanation:** First, Declaring the first complex vector. Declaring the second complex vector. Passing the input vectors to the dot function. * Mathematically, the dot product of complex vectors [5+2i 2+3i -4+i 3-6i 1+3i] and [5+i 4-2i 1+5i 2-3i 5-2i] is (53.0000 -56.0000i). As we can see in the output, we have obtained a dot product of our complex vectors (53.0000 -56.0000i), which is the same as expected by us.

#### Example #3

In this example, we will take 1 complex vector and will compute its dot product with a real vector using the dot function. For this example, we will input the following values:

1. Pass the complex vector as [3+2i 1+3i -5+i 3+3i ]

2. Pass the real vector as [5 -2 1 -5]

**Code:**

```
x = [3+2i 1+3i -5+i 3+3i ]
y = [5 -2 1 -5]
dot(x, y)
```

**Output:**

**Explanation: **First, Declaring the first input as a complex vector. Declaring the second input as a real vector. Passing the input vectors to the dot function. * Mathematically, the dot product of [3+2i 1+3i -5+i 3+3i ] and [5 -2 1 -5] is (-7.0000 +10.0000i). As we can see in the output, we have obtained a dot product of a complex vector with a real vector as a complex vector.

#### Example #4

In this example, we will take 2 matrices and will compute their dot product using the dot function. For this example, we will use 4×4 matrices as given below:

1. Pass the first matrix as [3 5 3 6; 4 1 6 0; 7 3 9 2];

2. Pass the second matrix as [1 0 3 5; 4 3 6 1; 7 1 3 0];

**Code:**

`x = [3 5 3 6; 4 1 6 0; 7 3 9 2];`

y = [1 0 3 5; 4 3 6 1; 7 1 3 0];

dot(x, y)

**Output:**

**Explanation: **First, Declaring the first input matrix. Declaring the second input matrix. Passing the input matrices to the dot function. * Mathematically, the dot product of matrix [3 5 3 6;4 1 6 0;7 3 9 2] and [1 0 3 5;4 3 6 1;7 1 3 0] is (68 6 72 30), As we can see in the output, we have obtained a dot product of our input matrices as (68 6 72 30), which is the same as expected by us.

### Conclusion

Dot function is used in MATLAB to find the dot product of vectors or scalars in MATLAB. For finding the dot product of matrices, the columns in the 1st matrix must be equal in number to rows in the 2nd matrix.

### Recommended Articles

This is a guide to Dot Product MATLAB. Here we discuss an introduction to Dot Product MATLAB, syntax, examples with sample code. You can also go through our other related articles to learn more –