Matrix product: inv or \ ?
Show older comments
I have four input matrixes and I'd like to do the simple following operation:
(Ygg - Ygl*inv(Ytotll)*Ylg)
where:
Ygg is 3x3 matrix
Ygl is 3x1000 matrix
Ylg is 1000x3 matrix
Yll is 1000x1000 matrix
- I noticed that the first two ways to do the operation are always faster and the third is the slower one. Why using \ is faster than inv? Is there a numerical reasone?
- Sometimes the first is faster than the second and sometimes the second is faster than the first. Is there any numerical-theoretical reasone about this behaviour?
Ygg = rand(3,3);
Ygl = rand(3,1000);
Ylg = rand(1000,3);
Yll = rand(1000,1000);
%Three different way to calculate the same product
tic;(Ygg - Ygl*(Yll\Ylg));toc
tic;(Ygg - (Ygl/Yll)*Ylg);toc
tic;(Ygg - Ygl*inv(Yll)*Ylg);toc
----------------------------------------------------------------------------------------
first run
Elapsed time is 0.022906 seconds.
Elapsed time is 0.023616 seconds.
Elapsed time is 0.059908 seconds.
second run
Elapsed time is 0.031426 seconds.
Elapsed time is 0.028280 seconds.
Elapsed time is 0.057623 seconds
third run
Elapsed time is 0.025449 seconds.
Elapsed time is 0.028944 seconds.
Elapsed time is 0.068196 seconds.
Accepted Answer
More Answers (0)
Categories
Find more on Linear Least Squares 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!