svd(X,"econ") does not work as expected if X is square nxn matrix with rank<n
4 views (last 30 days)
Show older comments
If X=[1 1;1 1] then with [U,S,V]=svd(X,"econ") I had expected that U=V=[1;1]/sqrt(2) and S=[2].
But "econ" does not work, S=[2 0;0 0] and U=V=[1 1;1 -1]/sqrt(2) (actually matlab gives opposite signs in U, but that does not matter).
What is the trick to make "econ" work in this case, so U is 2x1 and not 2x2 and S is 1x1 and not 2x2?
0 Comments
Accepted Answer
Steven Lord
on 25 Aug 2022
If you want a number of singular values fewer than the maximum number, consider using svds. Though for a tiny problem like this if you're expecting it to be faster than just calling svd the difference is likely to be negligible.
X=[1 1;1 1];
[U, S, V] = svds(X, 1) % Ask for the 1 largest singular value
4 Comments
Steven Lord
on 25 Aug 2022
[U, S, V] = svd(X, 'econ') returns a square S matrix with a number of rows and columns each equal to the minimum of the number of rows in X and the number of columns in X.
If X is "tall" (more rows than columns) the size of S is based on the number of columns.
If X is "wide" (more columns than rows) the size of S is based on the number of rows.
If X is square (the same number of rows and columns) the size of S is based on that common value. In this case, S is exactly the same size as X.
[U, S, V] = svd(X) makes S the exact same size as X regardless of whether X is "tall", "wide", or square.
So svd(X, 'econ') is behaving as it is documented to behave. If you have a use case for a different syntax for svd, one that is rank-based, you can file that as an enhancement request via Technical Support. When or if you file that enhancement request, please describe how you would use such functionality were it to be implemented. Enhancement requests with real-world use cases tend to carry more weight with the developers when considering what enhancement requests to action.
More Answers (2)
Chunru
on 25 Aug 2022
X=[1 1;1 1]
[U,S,V]=svd(X,"econ")
%I had expected that U=V=[1;1]/sqrt(2) and S=[2].
%But "econ" does not work, S=[2 0;0 0] and U=V=[1 1;1 -1]/sqrt(2), except for signs.
You can see svd with "econ" actually works. You expect U=V=[1;1]/sqrt(2) but another solution U=V=-[1;1]/sqrt(2) is actually the same solution (with opposite sign). It is obvious that X=U*S*V'=(-U)*S*(-V)'.
Bruno Luong
on 25 Aug 2022
Edited: Bruno Luong
on 25 Aug 2022
"[___] = svd(A,"econ") produces an economy-size decomposition of A using either of the previous output argument combinations. If A is an m-by-n matrix, then:
- m > n — Only the first n columns of U are computed, and S is n-by-n.
- m = n — svd(A,"econ") is equivalent to svd(A).
- m < n — Only the first m columns of V are computed, and S is m-by-m.
"
The "econ" option truncates the output when the matrix is not square based on matrix size. It does not do any truncation based on rank.
If you want to truncate on rank you can do it as post ptocessing step
r = rank(X); % r = find(diag(S)>=S(1,1)*eps,1,'last')
U = U(:,r);
S = S(1:r,1:r);
V = V(:,1:r);
2 Comments
Bruno Luong
on 25 Aug 2022
Edited: Bruno Luong
on 25 Aug 2022
You said "does not work as expected". You expect something that the doc NEVER claim to return.
So you did not read careful the doc.
Now back the question "why": Because the notion of numerical "rank" can vary, and possibly there is no universal truncation suitable for all application downstream. But I agree that it would be nice to have an adjustable option to truncate less than min(size(A)).
See Also
Categories
Find more on Linear Algebra 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!