Precision of Matlab Matrix Operators
Let's say we have these 3 matrices:
A=[1 3; 2 2]; B=[4 5; 1 3]; C=[0 2; 2 1]
I usually try to avoid using inv () or ^ (- 1) and try using the forward and backslash statements instead. But if I want to calculate the following:
A*B^(-1)*C
which way is best:
A/B*C
or
A*(B\C)
Even though both use slash operators and no "explicit inverse" is evaluated, the result is different and interestingly, A * (B \ C) computes the same as the one I am trying to avoid, / p>
A*inv(B)*C
and
isequal(A*(B\C),A*inv(B)*C)
shows. Can anyone please explain what is going on here and which way should I go? Thank!
source to share
MATLAB has a serious strategy mldivide
: see the Algorithm section of the documentation . I can imagine that it decides that 2x2 matrices with inverse transforms is the fastest way to solve it and hence it calls inv
and therefore explicitly computes the inverse B
. For large matrices this is not the case, and if I choose random isequal
4x4 matrices, doing -changes false.
In any case, the "best" approach is highly dependent on your matrices A
, B
and C
.
After some testing, I found it A*inv(B)*C
always takes longer than A*(B\C)
that, which means it can also be just machine precision (see Dang Khoa's helpful comment). However, there is no single "best" approach for mldivide
versus mrdivide
.
source to share