%% Cache Results from MATLAB Built-in Function
% To speed up performing a singular value decomposition when you could be operating on the same inputs multiple times, memoize the svd function.

fh = @svd;
memoizedFcn = memoize(fh);

% Create a matrix and cache the results of the singular value decomposition.  Time the function call.

X = magic(1234);
tic
[U,S,V]= memoizedFcn(X);
preCachedTime = toc

% Call the memoized function again using the same inputs.  To observe the speed improvement using cached results, time the function call again.

tic
[U,S,V]= memoizedFcn(X);
postCachedTime = toc

% Copyright 2012 The MathWorks, Inc.