%% Cache Results from User-Defined Function
% In your current working folder, create a file computeNumberCombinations.m that contains the following function to compute the number of combinations of n items taken k at a time.

type computeNumberCombinations.m

% Clear the cache for any MemoizedFunction objects in your workspace.

clearAllMemoizedCaches

% Memoize the  computeNumberCombinations function to speed up computation for repeated input values.

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

% Call the memoized function and time the function call.  This function call caches the results for the specified inputs.

tic
c = memoizedFcn(42e5,137);
preCachedTime = toc

% Call the memoized function and time the function call again.  This function call uses the cached results and does not execute the function.

tic
c = memoizedFcn(42e5,137);
postCachedTime = toc

% Copyright 2012 The MathWorks, Inc.