Hi mumin chy,
I understand that the goal is to inject known input-output pairs into the cache of a memoized function, so that when the function is called later with those inputs, the memoized function can return the known results without recomputing them.
However, the “memorize” function does not provide a way to directly modify the internal cache (memoizedFcn.stats.Cache.Inputs and memoizedFcn.stats.Cache.Outputs). These are read-only statistics and not meant to be manipulated for actual cache injection.
The workaround is to trick “memoize” into thinking it computed values like f(x1) without actually running the expensive function. Wrap f(x) in a function that:
- First checks if a known value for x exists (in “containers.Map”)
- If yes → return that known value (without calling f(x))
- If no → compute f(x) and let “memorize” store it normally
This ensures that computation of f(x1) or f(x2) will not happen again, and they are returned from the known values.
For more information on the MATLAB functions used above, please refer to the following documentation links:
