%% Specify Matrix Sketch Tolerance
% Specify a tolerance with svdsketch to compute the SVD factors of a low-rank matrix approximation. svdsketch adaptively determines the appropriate rank of the matrix sketch based on the specified tolerance.
%
% Use gallery to create a 200-by-200 random matrix with geometrically distributed singular values.

A = gallery('randsvd',200);

%
% Use svdsketch to calculate the SVD of a low-rank approximation of A. Specify a tolerance of 1e-2, and find the size of the output S to determine the rank svdsketch uses for the matrix sketch.

tol = 1e-2;
[U,S,V] = svdsketch(A,tol);
size(S)

% The results indicate that the low-rank matrix approximation of A has a rank of 60.
%
% Examine how well the matrix sketch approximates A by comparing A to U*S*V'.

norm(U*S*V' - A,'fro')/norm(A,'fro')

% The result indicates that the matrix sketch approximates A within the specified tolerance of 1e-2.
% Copyright 2020 The MathWorks, Inc.