92 views (last 30 days)
Show older comments
Mohammad Fatin Ishtiyaq on 9 Mar 2021
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
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
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
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
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
Or simply: A * B.'
Sign in to comment.
Sign in to answer this question.
Answers (2)
Sai Veeramachaneni on 12 Mar 2021
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
Show -2 older commentsHide -2 older comments
Sign in to comment.
Real User on 6 Feb 2023
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
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.
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