Vector Dot Product using Matrix (2024)

92 views (last 30 days)

Show older comments

Mohammad Fatin Ishtiyaq on 9 Mar 2021

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix

Edited: Real User on 6 Feb 2023

As we all know, the dot product of 2 vectors must be a scalar quantity. I have two vectors P and Z and they both have 6138 data points. So i converted them to Matrix of dimension 6138x3. Now when I used dot(P,Z) I am getting a 1x3 matrix. My question is how can I get the scalar quantity from this? Or Is there other way to the dot product of these two vectors using MATLAB.

THX

4 Comments

Show 2 older commentsHide 2 older comments

Stephen23 on 9 Mar 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1379041

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1379041

Edited: Stephen23 on 9 Mar 2021

Open in MATLAB Online

"the dot product of 2 vectors must be a scalar quantity."

Yes.

"I have two vectors P and Z and they both have 6138 data points. So i converted them to Matrix of dimension 6138x3."

Why did you convert one/both of the vectors to matrices?

If both P and Z have 6138 data points, where do all of the 6138*3=18414 data points come from?

"Now when I used dot(P,Z) I am getting a 1x3 matrix."

Which matches exactly what the dot documentation states: "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."

Lets try it (based on the documentation the expected output will be a 1x3 vector):

A = rand(6138,3);

B = rand(6138,3);

dot(A,B)

ans = 1×3

1.5283 1.5359 1.5163

Mohammad Fatin Ishtiyaq on 9 Mar 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1379066

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1379066

I meant P and Z both has 3 components of x,y,z . Thats why I converted it into matrix of 6138x3

Such as, P =2i+3j+6k ; 4i+5j+4k ; ...............upto 6138 data points .(same thing for Z)

Now how can i get the dot products of these 2 vectors.

I believe this is the simplified version of my question. Thanks

Stephen23 on 10 Mar 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1380867

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1380867

Edited: Stephen23 on 10 Mar 2021

Open in MATLAB Online

"Now how can i get the dot products of these 2 vectors."

Given that vectors are (by definition) specified by just two points, I don't see how your 6138 data points define just two vectors. Can you explain how those two vectors are defined by so many data points?

"I meant P and Z both has 3 components of x,y,z . Thats why I converted it into matrix of 6138x3"

You can specify the dimension argument to get the dot product of all 6138 pairs of vectors:

A = rand(6138,3);

B = rand(6138,3);

V = dot(A,B,2)

V = 6138×1

0.7055 0.8464 1.5368 1.1938 0.1349 0.7650 1.2069 0.3742 0.4781 0.9259

Jan on 10 Mar 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1381087

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#comment_1381087

Or simply: A * B.'

Sign in to comment.

Sign in to answer this question.

Answers (2)

Sai Veeramachaneni on 12 Mar 2021

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#answer_645798

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#answer_645798

Hi,

Refer to this example.

For multidimensional arrays, dot function calculates the dot product of corresponding vectors along the first array dimension whose size does not equal 1.

In your case I think sum(dot(A,B)) works for you.

Hope it helps.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Real User on 6 Feb 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#answer_1164620

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/767236-vector-dot-product-using-matrix#answer_1164620

Edited: Real User on 6 Feb 2023

Open in MATLAB Online

You have 6138 R^3 vectors in A and also in B (your coordinates of each vector run along the 2nd dim):

A = rand(6138,3);

B = rand(6138,3);

You want to get 6138 dot products:

[dot(A(1,:),B(1,:)); dot(A(2,:),B(2,:)); ...]

so write

dot(A,B,2) %the "2" says that the coordinates of each vector in A or B

% run along the second dim (the one with length 3).

%Note: "dot" is the conjugated inner product.

(Alternatively, originally have A=rand(3,6138) etc. or use its transpose A.'; similarly for B. Then dot(A,B) works. OR dot(A,B).', if you want to output a column vector, as above.)

If, instead, you want all 6138*6138 dot products, write:

A'*B %or A.'*B if you want the non-conjugating inner product. No difference if A & B are real.

[Credit to Jan & Stephen above.]

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsData TypesNumeric TypesLogical

Find more on Logical in Help Center and File Exchange

Tags

  • dot-product
  • matrices

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Vector Dot Product using Matrix (8)

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)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Vector Dot Product using Matrix (2024)

FAQs

How do you dot product a vector with a matrix? ›

If we multiply xT (a 1×n matrix) with any n-dimensional vector y (viewed as an n×1 matrix), we end up with a matrix multiplication equivalent to the familiar dot product of x⋅y: xTy=[x1x2x3⋯xn][y1y2y3⋮yn]=x1y1+x2y2+x3y3+…

How do you find the dot product of a vector? ›

Dot Product of Vectors

The scalar product of two vectors a and b of magnitude |a| and |b| is given as |a||b| cos θ, where θ represents the angle between the vectors a and b taken in the direction of the vectors.

What is the matrix method for vector product? ›

Matrix-vector product

So, if A is an m×n matrix (i.e., with n columns), then the product Ax is defined for n×1 column vectors x. If we let Ax=b, then b is an m×1 column vector. In other words, the number of rows in A (which can be anything) determines the number of rows in the product b.

What is the difference between matrix multiplication and dot product? ›

Matmul requires that the number of columns in the first matrix matches the number of rows in the second. The dot product, on the other hand, can be performed on vectors of any length.

What is the dot product rule for matrices? ›

Dot products are done between the rows of the first matrix and the columns of the second matrix. Thus, the rows of the first matrix and columns of the second matrix must have the same length. I want to emphasize an important point here: The length of a row is equal to the number of columns.

Does dot product commute with matrix multiplication? ›

The dot product is not commutative in general and the examples in the Documentation Center for Dot explicitly show this (Examples/Basic Examples/Products of matrices and vectors).

What is the answer to the dot product of two vectors? ›

The dot product of two vectors is the sum of the products of their corresponding components. It is the product of their magnitudes multiplied by the cosine of the angle between them. A vector's dot product with itself is the square of its magnitude.

What is the formula for the dot product of a vector projection? ›

The dot product of vectors a and unit vector u is the projection of a onto u, i.e., a⋅u=∥a∥cosθ, where θ is the angle between a and u. This expression does not involve the magnitude of u since it is normalized to be length one.

Why is cos theta used in dot products? ›

Dot products are all about projections. That is, calculating how much of one vector lies in the direction of the other. For this reason, the dot product has a cosine in the formula because it should be maximum when the angle between the vectors θ is zero.

What is the result of matrix vector product? ›

That is, the product of a matrix A (on the left) with a vector (on the right) is the linear combination of the columns of A obtained by using the entries of x as weights. We call Ax a product and use multiplicative notation for reasons that will become clear shortly.

How is vector related to matrix? ›

If a matrix has only one row or only one column it is called a vector. A matrix having only one row is called a row vector. is a row vector because it has only one row. A matrix having only one column is called a column vector.

How do you find the dot product of a matrix? ›

That is, the dot product is the summation of the products of the ith element of each vector. Note that for a dot product to be defined, both vectors must have the same number of elements. multiply them together, then sum the products. So A ∙ B = 1(4) + 3(5) + 4(2) = 4 + 15 + 8 = 27.

Does order matter in dot product matrix? ›

The dot product is commutative - the order of the vectors does not matter. a⋅b=b⋅a.

What is the rule for dot product? ›

Algebraic Properties of the Dot Product

(1) (Commutative Property) For any two vectors A and B, A.B = B.A. (2) (Scalar Multiplication Property) For any two vectors A and B and any real number c, (cA).B = A.(cB) = c(A.B) (3) (Distributive Property) For any 3 vectors A, B and C, A.(B+C) = A.B + A.C.

How do you express a vector in a matrix? ›

Definition. A k-by-1 matrix is called a column vector and a 1-by-k matrix is called a row vector. The coefficients in rowi of the matrix A determine a row vector Ai =(ai1, ai2, …,ain), and the coefficients of column j of A determine a column vector Aj = ha1 j,a2 j,...,amji.

How do you put a vector into a matrix? ›

To convert a vector into matrix, just need to use matrix function. We can also define the number of rows and columns, if required but if the number of values in the vector are not a multiple of the number of rows or columns then R will throw an error as it is not possible to create a matrix for that vector.

How can a vector be represented as a matrix? ›

Vectors as matrices

Because a vector is just a list of numbers, we can represent it as a matrix. This is why we can write vectors with matrix notation. We can always write a given vector as a row vector ( 1 × n ‍ ) or as a column vector ( n × 1 ‍ ).

References

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 5373

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.