# Vector Dot Product using Matrix

269 views (last 30 days)
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
Stephen23 on 10 Mar 2021
Edited: Stephen23 on 10 Mar 2021
"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
Or simply: A * B.'

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.

Real User on 6 Feb 2023
Edited: Real User on 6 Feb 2023
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.]