Main Content

dot

Dot product

Description

example

C = dot(A,B)returns thescalar dot productofAandB.

  • IfAandBare vectors, then they must have the same length.

  • IfAandBare matrices or multidimensional arrays, then they must have the same size. In this case, thedotfunction treatsAandBas 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 ofAandBalong dimension,dim. Thedim输入一个正整数标量。

Examples

collapse all

Create two simple, three-element vectors.

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

Calculate the dot product ofAandB.

C = dot(A,B)
C = 8

The result is8since

C = A(1)*B(1) + A(2)*B(2) + A(3)*B(3)

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 ofAandB.

C = dot(A,B)
C = 1.0000 - 5.0000i

The result is a complex scalar sinceAandBare 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 ofAwith 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).

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 ofAandB.

C = dot(A,B)
C =1×354 57 54

The result,C, contains three separate dot products.dottreats the columns ofAandBas vectors and calculates the dot product of corresponding columns. So, for example,C(1) = 54is the dot product ofA(:,1)withB(:,1).

Find the dot product ofAandB, treating therowsas vectors.

D = dot(A,B,2)
D =3×146 73 46

In this case,D(1) = 46is the dot product ofA(1,:)withB(1,:).

Create two multidimensional arrays.

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

Calculate the dot product ofAandBalong the third dimension (dim = 3).

C = dot(A,B,3)
C =2×2106 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 ofA(1,1,:)withB(1,1,:).

Input Arguments

collapse all

Input arrays, specified as numeric arrays.

Data Types:single|double
Complex Number Support:Yes

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,AandB:

  • dot(A,B,1)treats the columns ofAandB向量和返回t products of corresponding columns.

  • dot(A,B,2)treats the rows ofAandB向量和返回t products of corresponding rows.

dot(A,B,1) column-wise computation and dot(A,B,2) row-wise computation

dotreturnsconj(A).*Bifdimis greater thanndims(A).

More About

collapse all

Scalar Dot Product

The scalar dot product of two real vectors of lengthnis equal to

u · v = i = 1 n u i v i = u 1 v 1 + u 2 v 2 + ... + u n v n .

This relation is commutative for real vectors, such thatdot(u,v)equalsdot(v,u). If the dot product is equal to zero, thenuandvare 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 · v = i = 1 n u ¯ i v i .

Unlike the relation for real vectors, the complex relation is not commutative, sodot(u,v)equalsconj(dot(v,u)).

Algorithms

  • When inputsAandBare real or complex vectors, thedotfunction treats them as column vectors anddot(A,B)is the same assum(conj(A).*B).

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

Extended Capabilities

Version History

Introduced before R2006a