weird matrix multiplication with or without bracket
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I got a weird result of simple matrix multiplication. Theoretically, adding a bracket will not change result and R1=R2, but I have found R1 is not same as R2 by Matlab, especially when some columns of V are similar to each other. Do you know why they are different? and which one is closer to the true value?
Thanks
% V is K by M complex matrix, R is K by K complex matrix
R1=(V'*V)^(-0.5)*V'*R*V*(V'*V)^(-0.5)
R2=(V'*V)^(-0.5)*(V'*R*V)*(V'*V)^(-0.5)
0 commentaires
Réponses (2)
Stephen23
le 4 Août 2016
Modifié(e) : Stephen23
le 4 Août 2016
"Theoretically, adding a bracket will not change result"
Actually the two calculations are not equivalent, there is no reason why their outputs should be the same, and there is no "weird matrix multiplication" going on. These are perfectly normal floating point calculations.
The difference lies between these:
A*(B*C)
A*B*C
Are they the same? No.
MATLAB's operator precedence rules clearly state that brackets have the highest precedence of all, whereas operators of equal precedence are evaluated from left to right. So the above two calculations are actually evaluated as:
A*(B*C)
(A*B)*C
For floating point calculations these can give different results: this is the normal and expected behavior. Neither can be said to be "closer to the true value", because they are both floating point calculations and their errors may depend on many factors such as the magnitudes of the values, their differences, the specific operations, etc. As such, without an in-depth study of the that exact operation and the possible floating point error, it is not possible to say which might give a result closest to the related (but not the same) analytic calculation.
Lets try some values:
A = 0.9;
B = 0.1;
C = 1e20;
fprintf('%.0f\n',A*(B*C))
fprintf('%.0f\n',(A*B)*C)
fprintf('%.0f\n',A*B*C)
Prints this:
9000000000000000000
9000000000000001024
9000000000000001024
Note how the operations that I said are equivalent give the same result.
2 commentaires
Guillaume
le 5 Août 2016
Read the links from Walter's answer, and particularly read up about catastrophic cancellation.
Yes, the order of operations can result in vastly different results:
>> x = pi;
>> y = 1e20;
>> x + y - y %same as (x + y) - y
ans =
0
>> x + (y - y)
ans =
3.1416
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!