MATLAB: When solving for x in Ax=b, why is A\b faster that A^-1 * b

matrix algebra

if true
% code
A = [1,2,-1;2,5,-1;1,3,-3];
b = [6;13;4];
tic
for i=1:1000
X = A\b;
end
toc
tic
for i=1:1000
x = A^-1 * b;
end
toc
Elapsed time is 0.003520 seconds. Elapsed time is 0.017500 seconds.

Best Answer

Because \ is just one efficient command, rather than the two slow commands required with inv.
The inv documentation states "It is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving the system of linear equations Ax = b. One way to solve the equation is with x = inv(A)*b. A better way, from the standpoint of both execution time and numerical accuracy, is to use the matrix backslash operator x = A\b. This produces the solution using Gaussian elimination, without explicitly forming the inverse. See mldivide for further information."
So the documenation makes it clear that using mldivide is faster and produces better results. See also:
Related Question