RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • addprop
    • assignin
    • class
    • clear
    • clearAllMemoizedCaches
    • clearCache
    • clearvars
    • dbclear
    • dbstack
    • dbstatus
    • dbtype
    • evalc
    • feval
    • findprop
    • getcallinfo
    • inputParser
    • isa
    • ischar
    • isdeployed
    • iskeyword
    • ismethod
    • isobject
    • isstring
    • isUnderlyingType
    • keyboard
    • matlab.metadata.DynamicProperty.delete
    • memoize
    • metaclass
    • mislocked
    • mlock
    • munlock
    • namelengthmax
    • narginchk
    • nargoutchk
    • notify
    • onCleanup
    • stats
    • str2func
    • superclasses
    • underlyingType
    • verLessThan
    • version
    • which
    • who
    • whos

memoize — Create a MemoizedFunction handle object that caches function-handle results by input values and requested output count.

memoizedFcn = memoize(fh) returns a MemoizedFunction handle object for function handle fh. Calling the object reuses cached outputs when the input values and requested output count match a previous call. Repeated memoize calls for the same function handle share the same object state within the current runtime thread.

Syntax

memoizedFcn = memoize(fh)

Inputs

NameTypeRequiredDefaultDescription
fhAnyYes—Function handle to memoize.

Returns

NameTypeDescription
memoizedFcnAnyMemoizedFunction handle object.

Errors

IdentifierWhenMessage
RunMat:memoize:InvalidFunctionThe input is not a function handle or closure.memoize: input must be a function handle
RunMat:memoize:GcFailureThe memoized object target cannot be allocated or accessed.memoize: internal object storage failed

How memoize works

  • fh must be a function handle or anonymous-function closure.
  • The memoize constructor itself has no integer argument or result role and is explicitly integer-inapplicable. Integer values supplied later to the returned callable remain exact cache-key and cached-result payloads.
  • The returned object is callable like the original function through normal function-call syntax or feval.
  • Cache entries are keyed by the input values and the number of requested outputs.
  • Numeric NaN values compare equal for cache lookup.
  • Function stores the wrapped function handle and is read-only.
  • Enabled defaults to true. When false, calls dispatch to the wrapped function without reading, writing, or updating cache statistics.
  • CacheSize defaults to 10 and must be a positive integer scalar. The oldest entries are evicted when the cache exceeds this size.
  • clearCache(memoizedFcn) clears one shared cache and resets its counters.
  • clearAllMemoizedCaches clears every memoized-function cache in the current runtime thread.
  • stats(memoizedFcn) returns a structure with Cache.Inputs, Cache.Nargout, Cache.Outputs, Cache.HitCount, Cache.TotalHits, and Cache.TotalMisses.

Examples

Memoize an expensive function handle

f = memoize(@(x) sum(sin(x)));
y1 = f(1:1000);
y2 = f(1:1000);

Expected output:

% The second call reuses the cached result.

Inspect cache statistics

f = memoize(@sqrt);
f(4);
f(4);
S = stats(f)

Expected output:

% S.Cache.TotalHits is 1 and TotalMisses is 1.

Limit cache capacity

f = memoize(@cos);
f.CacheSize = 2;
f(1); f(2); f(3);

Expected output:

% The oldest cached entry is evicted.

Disable cache lookup temporarily

f = memoize(@rand);
f.Enabled = false;
a = f(1);
b = f(1);

Expected output:

% The two calls dispatch normally and do not update cache statistics.

Using memoize with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how memoize changes the result.

Run a small memoize example, explain the result, then change one input and compare the output.

FAQ

Does memoize(@f) return a function handle?⌄

No. It returns a MemoizedFunction handle object that can be invoked like the original function.

Do separate memoize(@f) calls share cache data?⌄

Yes, within the current runtime thread. The cache is associated with the wrapped function identity, so repeated memoization of the same function shares cache entries and properties.

Are multiple-output calls cached separately?⌄

Yes. The requested output count is part of the cache key.

Does this launch GPU kernels?⌄

No. memoize itself is host-side dispatch and bookkeeping. The wrapped function may still call GPU-aware operations.

Related Introspection functions

addprop · assignin · class · clear · clearAllMemoizedCaches · clearCache · clearvars · dbclear · dbstack · dbstatus · dbtype · evalc · feval · findprop · getcallinfo · inputParser · isa · ischar · isdeployed · iskeyword · ismethod · isobject · isstring · isUnderlyingType · keyboard · matlab.metadata.DynamicProperty.delete · metaclass · mislocked · mlock · munlock · namelengthmax · narginchk · nargoutchk · notify · onCleanup · stats · str2func · superclasses · underlyingType · verLessThan · version · which · who · whos

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how memoize is executed, line by line, in Rust.

  • View the source for memoize in Rust on GitHub
  • Learn how the RunMat runtime works
  • Found a bug? Open an issue with a minimal reproduction.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How memoize works
  • Examples
  • Memoize an expensive function handle
  • Inspect cache statistics
  • Limit cache capacity
  • Disable cache lookup temporarily
  • Using memoize with coding agents
  • FAQ
  • Related Introspection functions
  • Open-source implementation
  • About RunMat