-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvdecon.m
More file actions
43 lines (37 loc) · 757 Bytes
/
svdecon.m
File metadata and controls
43 lines (37 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function [U,S,V] = svdecon(X)
% Input:
% X : m x n matrix
%
% Output:
% X = U*S*V'
%
% Description:
% Does equivalent to svd(X,'econ') but faster
%
% Vipin Vijayan (2014)
%X = bsxfun(@minus,X,mean(X,2));
[m,n] = size(X);
if m <= n
C = X*X';
[U,D] = eig(C);
clear C;
[d,ix] = sort(abs(diag(D)),'descend');
U = U(:,ix);
if nargout > 2
V = X'*U;
s = sqrt(d);
V = bsxfun(@(x,c)x./c, V, s');
S = diag(s);
end
else
C = X'*X;
[V,D] = eig(C);
clear C;
[d,ix] = sort(abs(diag(D)),'descend');
V = V(:,ix);
U = X*V; % convert evecs from X'*X to X*X'. the evals are the same.
%s = sqrt(sum(U.^2,1))';
s = sqrt(d);
U = bsxfun(@(x,c)x./c, U, s');
S = diag(s);
end