Reputation: 31
Suppose we have these 3 matrices:
A=[1 3; 2 2]; B=[4 5; 1 3]; C=[0 2; 2 1]
Usually, I try to avoid using inv() or A^(-1) and try to use forward- and backslash operators instead. But if I want to calculate the following:
A*B^(-1)*C
which way is the best:
A/B*C
or
A*(B\C)
Although both use the slash operators and no "explicit inverse" is being calculated, the result is a different and interestingly, A*(B\C) calculates the same as the one I'm trying to avoid, which is
A*inv(B)*C
as
isequal(A*(B\C),A*inv(B)*C)
shows. Can anyone explain what is happening here and which way I should go? Thanks!
Upvotes: 3
Views: 223
Reputation: 1320
MATLAB has some serious strategy behind mldivide
: see the Algorithm section of the documentation. I can image that it decides that inversing matrices of size 2×2 is the quickest way for solving and hence it calls inv
and hence explicitely computes the inverse of B
. For larger matrices, this is not the case and if I pick random matrices of size 4×4, executing the isequal
-sentence returns false.
Anyway, the 'best' approach highly depends on your matrices A
, B
and C
.
After some testing, I found that A*inv(B)*C
always takes more time than A*(B\C)
, which means that it can also just be a machine-precision thing (see Dang Khoa's useful comment). Still, there is no single 'best' approach for mldivide
versus mrdivide
.
Upvotes: 0