Why is inv() better for symmetric matrices compared to '\'?
10 views (last 30 days)
Show older comments
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?
1 Comment
Jos (10584)
on 17 Feb 2016
The asymmetry probably relates to round-off errors occurring during the calculations used by the algorithm.
What do you mean by "better". Better for what? To calculate the inverse, use inv But for most other purposes, use the backslash operator
Accepted Answer
Matt J
on 17 Feb 2016
Edited: Matt J
on 17 Feb 2016
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?
0 Comments
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!