SVD functions have different results
2 views (last 30 days)
Show older comments
Hi guys, I want to implement SVD function with JAVA. First of all, I need to analyze the source codes of the SVD function on the matlab side. I found a third party SVD script. However, the results are different. The values are correct but the signs are different.
I have defined the following 5x5 matrix. I got the results with both build-in svd function and svdsimple script.
a = [0.9037 0.8909 0.3342 0.6987 0.1978;
0.0305 0.7441 0.5000 0.4799 0.9047;
0.6099 0.6177 0.8594 0.8055 0.5767;
0.1829 0.2399 0.8865 0.0287 0.4899;
0.1679 0.9787 0.7127 0.5005 0.4711];
[u1, s1, v1] = svd(a);
[u2, s2, v2] = svdsim(a);
As seen in the results, U and V matrices are different. The values are correct but the signs are wrong.
u1 =
-0.4608 0.7440 -0.0239 -0.1083 -0.4709
-0.4342 -0.4002 0.5913 0.4337 -0.3371
-0.5366 0.0790 -0.3882 0.4966 0.5554
-0.2947 -0.5137 -0.6325 -0.2448 -0.4349
-0.4736 -0.1268 0.3146 -0.7026 0.4086
u2 =
0.4608 -0.7440 0.0239 -0.1083 0.4709
0.4342 0.4002 -0.5913 0.4337 0.3371
0.5366 -0.0790 0.3882 0.4966 -0.5554
0.2947 0.5137 0.6325 -0.2448 0.4349
0.4736 0.1268 -0.3146 -0.7026 -0.4086
----------------------------------------------------------
v1 =
-0.3108 0.6276 -0.4916 0.1520 -0.4947
-0.5583 0.1761 0.5435 -0.5837 -0.1452
-0.4996 -0.4544 -0.6202 -0.3018 0.2612
-0.4217 0.3314 0.1520 0.4756 0.6805
-0.4053 -0.5087 0.2347 0.5647 -0.4505
v2 =
0.3108 -0.6276 0.4916 0.1520 0.4947
0.5583 -0.1761 -0.5435 -0.5837 0.1452
0.4996 0.4544 0.6202 -0.3018 -0.2612
0.4217 -0.3314 -0.1520 0.4756 -0.6805
0.4053 0.5087 -0.2347 0.5647 0.4505
I found SVD library for JAVA language. But the problem is the same, the values are correct but the signs are wrong. How can I implement the built-in svd function used in matlab?
0 Comments
Answers (1)
Steven Lord
on 19 May 2023
The values are correct but the signs are wrong.
That is not the correct way to say it. The signs are different but if you check both functions return valid singular value decompositions. The SVD of a matrix is not necessarily unique. From Wikipedia:
"Non-degenerate singular values always have unique left- and right-singular vectors, up to multiplication by a unit-phase factor e^(iφ) (for the real case up to a sign)."
rng default
A = randi(10, 5, 5);
[U, S, V] = svd(A);
format longg
check1 = A - (U*S*V') % All elements should be small in magnitude
U2 = -U;
V2 = -V;
check2 = A - (U2*S*V2')
U2 and U differ only in sign, and so do V2 and V. check1 and check2 show that both (U, S, V) and (U2, S, V2) are valid singular value decompositions of A.
0 Comments
See Also
Categories
Find more on Eigenvalues in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!