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
    • binocdf
    • boxplot
    • cdf
    • cdfplot
    • chi2cdf
    • corr
    • corrcoef
    • corrcov
    • cov
    • cov2corr
    • dummyvar
    • ecdf
    • filloutliers
    • fitdist
    • geomean
    • grpstats
    • harmmean
    • icdf
    • isoutlier
    • kstest
    • kurtosis
    • lsline
    • mad
    • mode
    • nanmax
    • normalize
    • normcdf
    • norminv
    • normpdf
    • onehotdecode
    • onehotencode
    • pdf
    • prctile
    • quantile
    • refline
    • rmse
    • skewness
    • tabulate
    • tcdf
    • tiedrank
    • tinv
    • tpdf
    • ttest2
    • wblinv

mode — Return most frequent values in MATLAB and RunMat.

mode(A) returns the most frequent value along a dimension. Tie-breaking, frequency outputs, and optional tied-value cell outputs follow MATLAB semantics.

Syntax

M = mode(X)
M = mode(X, dim_or_vecdim_or_all)
[M, F] = mode(X)
[M, F] = mode(X, dim_or_vecdim_or_all)
[M, F, C] = mode(X)
[M, F, C] = mode(X, dim_or_vecdim_or_all)

Inputs

NameTypeRequiredDefaultDescription
XAnyYes—Input data array.
dim_or_vecdim_or_allAnyYes—Reduction axis, vector of axes, or 'all'.

Returns

NameTypeDescription
MAnyMost frequent value along the selected dimension.
FNumericArrayFrequency counts for each reported mode.
CAnyCell array containing all tied modal values per slice.

Returned values from mode depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:mode:InvalidArgumentArguments are malformed, duplicated, or unrecognised.mode: invalid argument
RunMat:mode:InvalidDimensionDimension argument is zero or negative.mode: dimension must be >= 1
RunMat:mode:ComplexUnsupportedInput data is complex-valued.mode: complex inputs are not supported; gather real data first
RunMat:mode:InternalInternal conversion/allocation/shape handling fails.mode: internal operation failed

How mode works

  • mode(A) reduces along the first non-singleton dimension of A.
  • mode(A, dim) reduces along the specified dimension (1-based, >= 1).
  • mode(A, 'all') collapses every element of A into a single scalar mode.
  • [M, F, C] = mode(A, ...) additionally returns the frequency F of the mode (as a double) and a cell array C of sorted tied values per slice.
  • Ties resolve to the smallest tied value in M, while C contains the full tied set sorted in ascending order.
  • NaN entries are ignored. If a slice is entirely NaN (or empty), the corresponding M value is NaN, F is 0, and the cell entry is an empty 0 × 1 column.
  • Logical, integer, and boolean inputs are promoted to double precision before counting frequencies.
  • Dimensions larger than ndims(A) treat the input as having trailing singleton dimensions, so each slice contains a single element.

Does RunMat run mode on the GPU?

runmat-accelerate-api defines the provider-side mode_values contract for modal values, optional resident frequency counts, and optional sorted tied-set data for MATLAB cell output C, but the runtime does not route mode through it yet.

Future native GPU support should implement backend sorting/counting kernels behind that provider hook and then route supported real dense gpuArray forms through it rather than hiding sorting/counting logic in the runtime builtin.

GPU memory and residency

mode is an order-statistics residency sink today. GPU-resident real or logical inputs are gathered through the active provider, evaluated by the host MATLAB-compatible tie/frequency implementation, and returned as host values. Complex gpuArray inputs gather to host complex tensors and are rejected by the same complex-input diagnostic as host complex values.

Examples

Mode of a vector with a clear majority

x = [1 2 2 3 3 3 4];
m = mode(x)

Expected output:

m = 3

Ties return the smallest value with the full sorted set

x = [1 1 2 2];
[m, f, c] = mode(x)

Expected output:

m = 1
f = 2
c = {[1; 2]}

Column-wise mode of a matrix

A = [1 2 1; 2 2 3; 2 4 3];
m = mode(A)

Expected output:

m = [2 2 3]

Mode along the second dimension

A = [1 2 1; 2 2 3; 2 4 3];
m = mode(A, 2)

Expected output:

m = [1; 2; 2]

Mode across every element

A = [1 2 3; 2 3 2];
m = mode(A, 'all')

Expected output:

m = 2

NaN values are ignored unless every element is NaN

x = [1 NaN 2 2 NaN];
[m, f] = mode(x)

Expected output:

m = 2
f = 2

Using mode with coding agents

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

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

FAQ

How are ties resolved?⌄

M returns the smallest value among the tied set. The third output C contains every tied value sorted in ascending order as a column vector.

What happens with NaN values?⌄

NaN entries are skipped while counting frequencies. If a slice is entirely NaN, RunMat returns NaN for the mode, 0 for the frequency, and an empty 0 × 1 column in the cell array.

Does mode support logical or integer inputs?⌄

Yes. Logical and integer arrays are promoted to double precision before counting, matching MATLAB.

Can I reduce across all elements of an array?⌄

Use mode(A, 'all') to collapse every element of A into a single scalar.

Does mode execute on the GPU?⌄

Not today. GPU tensors are gathered to the host so the result remains bit-for-bit MATLAB compatible; future providers may expose dedicated mode kernels.

Related Stats functions

Summary

binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv

Ml

bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne

Random

binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd

Hist

histc · histcounts · histcounts2

Options

statget · statset

Open-source implementation

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

  • View the source for mode 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 mode works
  • Does RunMat run mode on the GPU?
  • GPU memory and residency
  • Examples
  • Mode of a vector with a clear majority
  • Ties return the smallest value with the full sorted set
  • Column-wise mode of a matrix
  • Mode along the second dimension
  • Mode across every element
  • NaN values are ignored unless every element is NaN
  • Using mode with coding agents
  • FAQ
  • Related Stats functions
  • Summary
  • Ml
  • Random
  • Hist
  • Options
  • Open-source implementation
  • About RunMat