Accelerating the pace of engineering and science

# dot

## Description

example

C = dot(A,B) returns the scalar dot product of A and B.

• If A and B are vectors, then they must have the same length.

• If A and B are matrices or multidimensional arrays, then they must have the same size. In this case, the dot function treats A and B as collections of vectors. The function calculates the dot product of corresponding vectors along the first array dimension whose size does not equal 1.

example

C = dot(A,B,dim) evaluates the dot product of A and B along dimension, dim. The dim input is a positive integer scalar.

## Examples

expand all

### Dot Product of Real Vectors

Create two simple, three-element vectors.

```A = [4 -1 2];
B = [2 -2 -1];```

Calculate the dot product of A and B.

`C = dot(A,B)`
```C =

8```

The result is 8 since

$\begin{array}{l}C=A\left(1\right)*B\left(1\right)+A\left(2\right)*B\left(2\right)+A\left(3\right)*B\left(3\right)\\ C=8+2-2\text{\hspace{0.17em}}\text{\hspace{0.17em}}.\end{array}$

### Dot Product of Complex Vectors

Create two complex vectors.

```A = [1+i 1-i -1+i -1-i];
B = [3-4i 6-2i 1+2i 4+3i];```

Calculate the dot product of A and B.

`C = dot(A,B)`
```C =

1.0000 - 5.0000i```

The result is a complex scalar since A and B are complex. In general, the dot product of two complex vectors is also complex. An exception is when you take the dot product of a complex vector with itself.

Find the inner product of A with itself.

`D = dot(A,A)`
```D =

8```

The result is a real scalar. The inner product of a vector with itself is related to the Euclidean length of the vector, norm(A).

### Dot Product of Matrices

Create two matrices.

```A = [1 2 3;4 5 6;7 8 9];
B = [9 8 7;6 5 4;3 2 1];```

Find the dot product of A and B.

`C = dot(A,B)`
```C =

54    57    54```

The result, C, contains three separate dot products. dot treats the columns of A and B as vectors and calculates the dot product of corresponding columns. So, for example, C(1) = 54 is the dot product of A(:,1) with B(:,1).

Find the dot product of A and B, treating the rows as vectors.

`D = dot(A,B,2)`
```D =

46
73
46```

In this case, D(1) = 46 is the dot product of A(1,:) with B(1,:).

### Dot Product of Multidimensional Arrays

Create two multidimensional arrays.

```A = cat(3,[1 1;1 1],[2 3;4 5],[6 7;8 9])
B = cat(3,[2 2;2 2],[10 11;12 13],[14 15; 16 17])```
```A(:,:,1) =

1     1
1     1

A(:,:,2) =

2     3
4     5

A(:,:,3) =

6     7
8     9

B(:,:,1) =

2     2
2     2

B(:,:,2) =

10    11
12    13

B(:,:,3) =

14    15
16    17```

Calculate the dot product of A and B along the third dimension (dim = 3).

`C = dot(A,B,3)`
```C =

106   140
178   220```

The result, C, contains four separate dot products. The first dot product, C(1,1) = 106, is equal to the dot product of A(1,1,:) with B(1,1,:).

## Input Arguments

expand all

### A,B — Input arraysnumeric arrays

Input arrays, specified as numeric arrays.

Data Types: single | double
Complex Number Support: Yes

### dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, the default is the first array dimension whose size does not equal 1.

Consider two 2-D input arrays, A and B:

• dot(A,B,1) treats the columns of A and B as vectors and returns the dot products of corresponding columns.

• dot(A,B,2) treats the rows of A and B as vectors and returns the dot products of corresponding rows.

dot returns conj(A).*B if dim is greater than ndims(A).

expand all

### Scalar Dot Product

The scalar dot product of two real vectors of length n is equal to

$u\text{\hspace{0.17em}}·\text{\hspace{0.17em}}v=\sum _{i=1}^{n}{u}_{i}{v}_{i}={u}_{1}{v}_{1}+{u}_{2}{v}_{2}+...+{u}_{n}{v}_{n}\text{\hspace{0.17em}}.$

This relation is commutative for real vectors, such that dot(u,v) equals dot(v,u). If the dot product is equal to zero, then u and v are perpendicular.

For complex vectors, the dot product involves a complex conjugate. This ensures that the inner product of any vector with itself is real and positive definite.

$u\text{\hspace{0.17em}}·\text{\hspace{0.17em}}v=\sum _{i=1}^{n}{\overline{u}}_{i}{v}_{i}\text{\hspace{0.17em}}.$

Unlike the relation for real vectors, the complex relation is not commutative, so dot(u,v) equals conj(dot(v,u)).

### Algorithms

• When inputs A and B are real or complex vectors, the dot function treats them as column vectors and dot(A,B) is the same as sum(conj(A).*B).

• When the inputs are matrices or multidimensional arrays, the dim argument determines which dimension the sum function operates on. In this case, dot(A,B) is the same as sum(conj(A).*B,dim).