MATLAB: Is inv() better for symmetric matrices compared to ‘\’

invissymmetricMATLABmldividesymmetric

I'm interested in calculating alot of covariance matrices, which all require an inversion of a symmetric matrix, and therefore the output should be symmetric. I've always been taught that using the backslash operator is better than using inv() or ^(-1), i.e.
Ainv = A\eye(size(A,1))
BUT! If I run the following code,
P = 1000;
pp = randn(P);
A = pp*pp';
issymmetric(A)
issymmetric(inv(A))
issymmetric(A\eye(P))
this yields that inv() returns a symmetric matrix but '\' doesnt. Why is this the case?

Best Answer

I've always been taught that using the backslash operator is better than using inv() or ^(-1)
What you were taught, and maybe misunderstood, is that backslash is better for solving systems of linear equations, and does so by avoiding matrix inversion altogether. For one thing, this ends up being a fair bit faster:
P=3000;
pp = randn(P);
A = pp*pp'; b=rand(P,1);
>> tic; inv(A)*b; toc
Elapsed time is 0.634390 seconds.
>> tic; A\b; toc
Elapsed time is 0.156036 seconds.
However, if you really do need the full matrix inverse, then no reason not to use INV, as far as I know.
That said, Jos' comment still stands. The fact that A\eye(N) gives a slightly less symmetric result is not a clear sign of superiority. So what if the result is a few epsilons away from symmetric?
Related Question