Why is inv() better for symmetric matrices compared to '\'?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Søren Føns Vind Nielsen
el 17 de Feb. de 2016
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 comentario
Jos (10584)
el 17 de Feb. de 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
Respuesta aceptada
Matt J
el 17 de Feb. de 2016
Editada: Matt J
el 17 de Feb. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Linear Algebra en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!