Dot product
collapse all in page
Syntax
C = dot(A,B)
C = dot(A,B,dim)
Description
example
C = dot(A,B)
returnsthe scalar dotproduct of A
and B
.
If
A
andB
arevectors, then they must have the same length.If
A
andB
arematrices or multidimensional arrays, then they must have the samesize. In this case, thedot
function treatsA
andB
ascollections of vectors. The function calculates the dot product ofcorresponding vectors along the first array dimension whose size doesnot equal 1.
example
C = dot(A,B,dim)
evaluatesthe dot product of A
and B
alongdimension, dim
. The dim
inputis a positive integer scalar.
Examples
collapse all
Dot Product of Real Vectors
Open Live Script
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
C = A(1)*B(1) + A(2)*B(2) + A(3)*B(3)
Dot Product of Complex Vectors
Open Live Script
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
Open Live Script
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 = 1×3 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 = 3×1 46 73 46
In this case, D(1) = 46
is the dot product of A(1,:)
with B(1,:)
.
Dot Product of Multidimensional Arrays
Open Live Script
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 1A(:,:,2) = 2 3 4 5A(:,:,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 2B(:,:,2) = 10 11 12 13B(:,:,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 = 2×2 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
collapse all
A,B
— Input arrays
numeric arrays
Input arrays, specified as numeric arrays.
Data Types: single
| double
Complex Number Support: Yes
dim
— Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integerscalar. If no value is specified, the default is the first array dimensionwhose size does not equal 1.
Consider two 2-D input arrays, A
and B
:
dot(A,B,1)
treats the columnsofA
andB
as vectors and returnsthe dot products of corresponding columns.dot(A,B,2)
treats the rows ofA
andB
asvectors and returns the dot products of corresponding rows.
dot
returns conj(A).*B
if dim
isgreater than ndims(A)
.
More About
collapse all
Scalar Dot Product
The scalar dot product of two real vectorsof length n is equal to
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 areperpendicular.
For complex vectors, the dot product involves a complex conjugate.This ensures that the inner product of any vector with itself is realand positive definite.
Unlike the relation for real vectors, the complex relation isnot commutative, so dot(u,v)
equals conj(dot(v,u))
.
Algorithms
When inputs
A
andB
arereal or complex vectors, thedot
function treatsthem as column vectors anddot(A,B)
is the sameassum(conj(A).*B)
.When the inputs are matrices or multidimensional arrays,the
dim
argument determines which dimension thesum
functionoperates on. In this case,dot(A,B)
is the sameassum(conj(A).*B,dim)
.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with thelimitation:
For the syntax dot(A,B)
, the arrays A
and B
musthave the same size, even if they are vectors.
For more information, see Tall Arrays for Out-of-Memory Data.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
cross | sum | conj | norm | tensorprod
External Websites
- Applied Linear Algebra (MathWorks Teaching Resources)
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office