Numerical precision of matrix multiplication (inconsistent results with colon indexing)
1 view (last 30 days)
Show older comments
Why do the two ways of matrix multiplication lead to different results?
rng(1);
A = randn(100,3);
B = A'*A;
C = A(:,:)'*A(:,:);
B-C
results in
ans =
1.0e-13 *
0 -0.0178 0.0133
-0.0178 -0.1421 -0.0133
0.0133 0.0311 0.1421
The difference is not huge, but still I don't understand why the results differ at all.
0 Comments
Answers (1)
Jos (10584)
on 27 Feb 2014
Edited: Jos (10584)
on 27 Feb 2014
Strange! Also strange:
rng(1) ; A = randn(100,3) ;
B1 = A'*A
B2 = mtimes(A',A)
disp(['B1 == B2 : ' '0'+isequal(B2, B1)]) ; % ?????
% but
X = A' ;
C1 = X*A
C2 = mtimes(X,A)
disp(['C1 == C2 : ' '0'+isequal(C1, C2)]) ; % YES!
disp(['C1,C2 == B2 : ' '0'+isequal(C1, C2, B2)]) ;
I first thought that your C is created using MTIMES:
C = A(:,:)'*A(:,:)
disp(['C == B2 : ' '0'+isequal(C, B2)]) ; % but no
disp(['C == B1 : ' '0'+isequal(C, B1)]) ; % also not!!
So there are at least three different results, that should be the same ...
Very puzzling!
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!