mode — Most frequent value along a dimension with MATLAB-compatible tie semantics.
mode(A) returns the value that appears most frequently in A. When multiple values share the same maximum frequency, the smallest tied value is returned in M, matching MATLAB. Requesting [M, F, C] = mode(A) additionally yields the frequency of the mode and a cell array C whose entries are sorted column vectors of the tied values for each slice.
How mode works
mode(A)reduces along the first non-singleton dimension ofA.mode(A, dim)reduces along the specified dimension (1-based,>= 1).mode(A, 'all')collapses every element ofAinto a single scalar mode.[M, F, C] = mode(A, ...)additionally returns the frequencyFof the mode (as a double) and a cell arrayCof sorted tied values per slice.- Ties resolve to the smallest tied value in
M, whileCcontains the full tied set sorted in ascending order. - NaN entries are ignored. If a slice is entirely
NaN(or empty), the correspondingMvalue isNaN,Fis0, and the cell entry is an empty0 × 1column. - 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.
Examples
Mode of a vector with a clear majority
x = [1 2 2 3 3 3 4];
m = mode(x)Expected output:
m = 3Ties 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 = 2NaN values are ignored unless every element is NaN
x = [1 NaN 2 2 NaN];
[m, f] = mode(x)Expected output:
m = 2
f = 2FAQ
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
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mode works, line by line, in Rust.
- View mode.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.