File Exchange

## Fast SVD and PCA

version 1.3.0.0 (3.35 KB) by
Fast truncated SVD and PCA rectangular matrices

Updated 07 Jul 2014

View Version History

Truncated Singular Value Decomposition (SVD) and Principal Component Analysis (PCA) that are much faster compared to using the Matlab svd and svds functions for rectangular matrices.
svdecon is a faster alternative to svd(X,'econ') for long or thin matrices.
svdsecon is a faster alternative to svds(X,k) for dense long or thin matrices where k << size(X,1) and size(X,2).
PCA versions of the two svd functions are also implemented.
---

function [U,S,V] = svdecon(X)
function [U,S,V] = svdecon(X,k)

Input:
X : m x n matrix
k : gets the first k singular values (if k not given then k = min(m,n))

Output:
X = U*S*V'
U : m x k
S : k x k
V : n x k

Description:
svdecon(X) is equivalent to svd(X,'econ')
svdecon(X,k) is equivalent to svds(X,k) where k < min(m,n)
This is faster than svdsecon when k is not much smaller than min(m,n)

---

function [U,S,V] = svdsecon(X,k)

Input:
X : m x n matrix
k : gets the first k singular values, k << min(m,n)

Output:
X = U*S*V' approximately (up to k)
U : m x k
S : k x k
V : n x k

Description:
svdsecon(X,k) is equivalent to svds(X,k) where k < min(m,n)
This function is useful if k << min(m,n) (see doc eigs)

---

function [U,T,mu] = pcaecon(X,k)

Input:
X : m x n matrix
Each column of X is a feature vector
k : extracts the first k principal components

Output:
X = U*T approximately (up to k)
T = U'*X
U : m x k
T : k x n

Description:
Principal Component Analysis (PCA)
Requires that k < min(m,n)

---

function [U,T,mu] = pcasecon(X,k)

Input:
X : m x n matrix
Each column of X is a feature vector
k : extracts the first k principal components, k << min(m,n)

Output:
X = U*T approximately (up to k)
T = U'*X
U : m x k
T : k x n

Description:
Principal Component Analysis (PCA)
Requires that k < min(m,n)
This function is useful if k << min(m,n) (see doc eigs)

### Cite As

Vipin Vijayan (2020). Fast SVD and PCA (https://www.mathworks.com/matlabcentral/fileexchange/47132-fast-svd-and-pca), MATLAB Central File Exchange. Retrieved .

Antoine Klauser

I ran the testsvd.m script and the computing times are exactly the same between the built in function and the proposed scripts.
Maybe Matlab built in functions have been optimized since the publication of this code...

Mark Wagner

Code is well written, but it's easy to demonstrate that it works nearly an order of magnitude slower than the inbuilt MATLAB svd() function

Giuseppe Di Massa

Xu Jun

It has bug which generate NaN or Inf in the 'svdecon.m' function.

Lifang Yu

I'm working on spliting an image into many small matrix, so very fast svd on small size matrix is what I need. This svd implementaion is lower than Matlab's svd when processing small size matrix. I don't try it on matrix with large size.

khthung

Thanks, it works for me and it does not have convergence problem when I run it in linux server. Matlab built-in svd function will give me convergence error.

##### MATLAB Release Compatibility
Created with R2013a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux