Dot product - MATLAB dot - MathWorks United Kingdom (2024)

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 and B arevectors, then they must have the same length.

  • If A and B arematrices or multidimensional arrays, then they must have the samesize. In this case, the dot function treats A and B 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

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,BInput arrays
numeric arrays

Input arrays, specified as numeric arrays.

Data Types: single | double
Complex Number Support: Yes

dimDimension 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 columnsof A and B as vectors and returnsthe dot products of corresponding columns.

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

Dot product - MATLAB dot- MathWorks United Kingdom (1)

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

u·v=i=1nuivi=u1v1+u2v2+...+unvn.

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.

u·v=i=1nu¯ivi.

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

Algorithms

  • When inputs A and B arereal or complex vectors, the dot function treatsthem as column vectors and dot(A,B) is the sameas sum(conj(A).*B).

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

Extended Capabilities

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (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.

Dot product - MATLAB dot- MathWorks United Kingdom (2)

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)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Dot product - MATLAB dot
- MathWorks United Kingdom (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5575

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.