dot product between system of vectors
10 views (last 30 days)
Show older comments
bidlee devi
on 26 Jan 2020
Commented: Turlough Hughes
on 26 Jan 2020
Suppose we have a system of vectors Z and Y of size 3 *3 consisitng of three column vectors with three tuples in each colunm.
Z=[45 33 37 ; 39 28 37 ;35 36 48 ]
Y=[ 29.5 30 30 ;29.51 30 30 ;29.51 30 29 ]
How to write a matlab code to get Q of size 3*3 given by
Q=[10000.88925 9410.822 10295.99 ;10001.81888 9411.39 10296.72 ;10000.49116 9410.226 10295.24 ]
Here, in Q , element in (1,1) is a dot product between first column of Z and first column of Y. Likewise element in (2,1) is a dot product of second column of Y and first column of Z so on element (3,1) is a dot product between first column of Z and third column of Y. In this way, the first column of Q is formed. Moreover, for example, element (2,3) is a dot product between second column of Y and third column of Z and so forth
Can anyone help me out on how to automate this using matlab codes ? urgently needed.
Thanks in advance.
1 Comment
Accepted Answer
Thiago Henrique Gomes Lobato
on 26 Jan 2020
How you are getting these values for Q? If you calculate the dot product between the colums of the variables you give the values are different, maybe you had a small error on your code? Back to your question this is basically a transpose matrix multiplication. A normal matrix multiplication is basically the dot product between rows and columns, if you transpose the first matrix you get the dot product of columns and columns, as you want. As example:
Z=[45 33 37 ; 39 28 37 ;35 36 48 ];
Y=[ 29.5 30 30 ;29.51 30 30 ;29.51 30 29 ];
A1 = zeros(3);
% Loop to do column dot product
for idxX = 1:3
for idxY = 1:3
A1(idxX,idxY) = dot(Z(:,idxX),Y(:,idxY));
end
end
A2 = Z'*Y; % Transpose matrix multiplication
A1
A2
norm(A1-A2)
A1 =
1.0e+03 *
3.5112 3.5700 3.5350
2.8621 2.9100 2.8740
3.5999 3.6600 3.6120
A2 =
1.0e+03 *
3.5112 3.5700 3.5350
2.8621 2.9100 2.8740
3.5998 3.6600 3.6120
ans =
6.4311e-13
2 Comments
Turlough Hughes
on 26 Jan 2020
Q = Z.'*Y
is actually the transpose of what was described, the correct answer is:
Q = Y.'*Z
More Answers (3)
Turlough Hughes
on 26 Jan 2020
Edited: Turlough Hughes
on 26 Jan 2020
Based on your description you can do the following, transpose the Y and then it's a straight forward case of matrix multiplication
Q = Y.'*Z;
Though your description doesn't match the results you provide in Q. For example the dot product of the first columns is not 10000.88925
dot(Z(:,1),Y(:,1))
0 Comments
See Also
Categories
Find more on Software Development Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!