Search code examples
matlabmatrix-multiplicationmatrix-inverse

inv(A)*B vs A\B - Why this weird behavior in MatLab?


Lets create two random matrices,

A = randn(2)
B = randn(2)

both inv(A)*B and A\B give the same result

inv(A)*B
A\B

ans =

    0.6175   -2.1988
   -0.7522    5.0343

ans =

    0.6175   -2.1988
   -0.7522    5.0343

unless I multiply with some factor. Why is this?

.5*A\B
.5*inv(A)*B

ans =

    1.2349   -4.3977
   -1.5045   10.0685

ans =

    0.3087   -1.0994
   -0.3761    2.5171

This is very annoying since MatLab always nudges me to use A\B instead of inv(A)*B and it took me years to figure out why my code was not working.


Solution

  • When A is non-singular matrix, then inv(A) * B = A \ B.

    Your calculation is as follows: .5 * A\B = (0.5 * A) \ B vs .5* inv(A) * B = 0.5 * (A\B) . As such, it will give your unequal result.