%
% Compute the CUR decomposition of matrix A. 
% 
% Note that the code is not written to optimize the cpu time
%
%--------------------------------------------------------------
%
% generate the test matrix A

m = 3000; 
n = 300; 
A = randn(m,n); 

% compute the svd of A
[UA,SA,VA] = svd(A); 

% compute the CUR approximation 

bound = n*log(n)/m;   % key parameter 
sampled_rows = find(rand(m,1) < bound); 
R = A(sampled_rows,:); 
sampled_cols = find(rand(n,1) < bound); 
C = A(:,sampled_cols); 
U = pinv(C)*A*pinv(R); 

% rank-k truncated SVD 
k = rank(U) 
Ak = UA(:,1:k)*SA(1:k,1:k)*VA(:,1:k)';  

% compare the error ratio
Errorratio = norm(A - C*U*R)/norm(A - Ak) 

