How does Matlabs svd() function calculate the value of V?
97 views (last 30 days)
Show older comments
I have been attempting to recreate the singular value decomposition in Matlab without the use of the svd() function. I have been able to get the values of S and U, but I have had issues recreating the results of V. I have been using the definition of V= orthogonal basis of X' * X, where X has dimensions of M * N, represented in code as
V=orth(transpose(X) * X);
However, the output of this is a N * M matrix, while the expected dimensions (and the dimensions of V in the svd function) is M * M. I seem to be missing the last colum of the matrix consistantly. Am I misunderstanding the theory behind svd, or is there an issue with the orth() command?
12 Comments
Paul
on 15 Jun 2025 at 21:30
Hi David,
According to the doc page at eig - Eigenvalues and eigenvectors - MATLAB, "If A is real symmetric, Hermitian, or skew-Hermitian, then the right eigenvectors V [returned by eig] are orthonormal."
Does that assuage concerns about v?
I added the part in brackets because, it seems to me, that the eigenvectors associated with repeated eigenvalues of a symmetric matrix don't have to be orthonormal, though it looks like eig does enforce that for the single input case, e.g.,
Z = zeros(2);
[V,D] = eig(Z)
[V,D] = eig(eye(2))
I am curious as to how eig() works for symmetric matrices. Does it use issymmetric (or equivalent) on the input and take different paths depending on the result? Also, is M = A'*A guaranteed to always be symmetric in floating point (I thought that Matlab will magically ensure that M is symmetric, but I can't find anything on point in the doc).
The two-input form of eig is more interesting (I readily confess to not being that familiar with the generalized eigenvalue problem)
[V,D] = eig(Z,Z,'chol')
[V,D] = eig(Z,Z,'qz')
I don't know what to make of the D-matrix, insofar as any finite D and any finite, non-zero V will satisfy Z*V = Z*V*D
David Goodmanson
on 17 Jun 2025 at 23:49
Edited: David Goodmanson
on 18 Jun 2025 at 0:43
Hi Paul, as I said I didn't doubt that Matlab eig (with one input) produces an orthonormal matrix for V, whether the eigenvalues are repeated or not. I wasn't positive if in the repeated case, the resulting u from
u = A*v/s
would always be correct. But now I have verified it both by proof and example. So the initial caveat I had is unnecessary.
In what you posted above, is Z still hanging around as zeros(2) from the first line? Then it will be hard to construct anything out of eig(Z,Z).
Answers (2)
John D'Errico
on 10 Jun 2025 at 18:12
Edited: John D'Errico
on 10 Jun 2025 at 18:15
UM, You seriously do not want to use X'*X to compute anything about the SVD. Yes, mathematically, it is the same. But this is not the case computationally!!!!!!
How does MATLAB compute the singular vectors? It probably calls code from LAPACK, as I recall. When I wrote my own SVD code a million years ago, based on the old LINPACK algorithms, the idea was to bidiagonalize the matrix using Householder transformations. Then I recall I needed to do Givens rotations to finish things off, killing off the off-diagonal elements one at a time. (Its been a long time since I needed an SVD, so that is just my fading memory.) You can find the basic algorithm I used back then in the original LINPACK manual.
The point is, at no time do you EVER want to form X'*X. The problem is, once you do that, you destroy any information available down in the lower significant bits of your array. And this is why, when you tried to do it, those small singular values (and their associated singular vectors) are complete crap. Worthless.
1 Comment
James Tursa
on 14 Jun 2025 at 1:08
Edited: James Tursa
on 14 Jun 2025 at 1:11
E.g., some LAPACK documentation for SVD computation routines can be found on netlib:
Christine Tobler
on 10 Jun 2025 at 19:08
Edited: Christine Tobler
on 10 Jun 2025 at 19:08
Hi Caleb,
Happy to discuss the svd with you!
That being said, as others here have mentioned, svd is a building block and not easy to implement. Things like orth, rank, null, pinv all are built on top of the SVD, and so wouldn't be an independent way of computing the SVD.
There are some ways to write up the SVD through symmetricEIG, but then EIG is similarly a building block and hard to implement - so it's a bit like going in circles.
What is your final goal? Would you like to understand how the SVD is computed? Or are you looking at a specific problem where you are currently using the SVD?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!