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
    • argsort
    • intersect
    • ismember
    • ismembertol
    • issorted
    • issortedrows
    • setdiff
    • setxor
    • sort
    • sortrows
    • union
    • unique

unique — Return unique elements or rows with MATLAB-compatible ordering and index mappings.

unique(A) removes duplicates and returns distinct elements or rows. It supports MATLAB-compatible sorted/stable ordering plus optional index outputs linking original and unique values.

Syntax

C = unique(A)
C = unique(A, option...)
[C, ia] = unique(A)
[C, ia] = unique(A, option...)
[C, ia, ic] = unique(A)
[C, ia, ic] = unique(A, option...)

Inputs

NameTypeRequiredDefaultDescription
AAnyYes—Input array.
optionAnyVariadic—Option tokens plus the 'TreatMissingAsDistinct', logical name-value pair.

Returns

NameTypeDescription
CAnyUnique values or rows.
iaNumericArrayIndices selecting representatives in input A.
icNumericArrayIndices mapping each input element/row to C.

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

Errors

IdentifierWhenMessage
RunMat:unique:LegacyOptionUnsupportedLegacy compatibility options are requested.unique: the 'legacy' behaviour is not supported
RunMat:unique:ConflictingOrderOptionsBoth 'sorted' and 'stable' options are provided.unique: cannot combine 'sorted' with 'stable'
RunMat:unique:ConflictingOccurrenceOptionsBoth 'first' and 'last' options are provided.unique: cannot combine 'first' with 'last'
RunMat:unique:UnknownOptionAn unsupported option token is provided.unique: unrecognised option
RunMat:unique:RowsRequiresTwoDimensionalInput'rows' mode is used with non-2D data.unique: 'rows' option requires a 2-D matrix input
RunMat:unique:UnsupportedInputTypeInput cannot be converted into a supported unique domain.unique: unsupported input type
RunMat:unique:InvalidArgumentOption arguments or name-value pairs are malformed.unique: invalid option arguments
RunMat:unique:GpuOptionCombinationA resident input specifies both set-order and occurrence options.unique: GPU inputs cannot combine a set-order option with 'first' or 'last'
RunMat:unique:InternalInternal conversion/allocation/provider decode fails.unique: internal operation failed

How unique works

  • unique(A) flattens numeric, logical, character, string, or complex arrays column-major into a vector of unique values sorted ascending.
  • [C, IA] = unique(A) also returns the indices of the selected occurrences (IA) so that C = A(IA).
  • [C, IA, IC] = unique(A) provides IC, the mapping from each element of A to the corresponding index in C.
  • unique(A, 'stable') preserves the first appearance order rather than sorting.
  • unique(A, 'rows') treats each row as an observation and returns a matrix (or char/string array) whose rows are unique.
  • unique(A, 'last') or 'first' controls which occurrence contributes to IA (defaults to 'first').
  • Combinations such as unique(A, 'rows', 'stable', 'last') follow MATLAB's precedence rules; mutually exclusive flags (e.g. 'sorted' with 'stable') are rejected.
  • Empty inputs return empty outputs with consistent dimensions.
  • Legacy switches such as 'legacy' or 'R2012a' are not supported; RunMat always follows the modern MATLAB semantics.

Does RunMat run unique on the GPU?

unique is registered as a residency sink. When the provider exposes the custom unique hook, the runtime can execute the operation entirely on the device and keep results resident. If the active provider does not implement that hook, RunMat gathers the data to host memory, performs the CPU implementation, and returns host-resident outputs so subsequent MATLAB code observes the same values and ordering.

Examples

Getting Sorted Unique Values

A = [3 1 3 2];
C = unique(A)

Expected output:

C =
     1
     2
     3

Preserving Input Order with 'stable'

A = [4 2 4 1 2];
C = unique(A, 'stable')

Expected output:

C =
     4
     2
     1

Returning Indices for Reconstruction

A = [7 5 7 3];
[C, IA, IC] = unique(A);
reconstructed = C(IC)

Expected output:

C =
     3
     5
     7
IA =
     4
     2
     1
IC =
     3
     2
     3
     1
reconstructed =
     7
     5
     7
     3

Finding Unique Rows in a Matrix

A = [1 3; 1 3; 2 4; 1 2];
[C, IA, IC] = unique(A, 'rows')

Expected output:

C =
     1     2
     1     3
     2     4
IA =
     4
     1
     3
IC =
     2
     2
     3
     1

Selecting Last Occurrences

A = [9 8 9 7 8];
[C, IA] = unique(A, 'last')

Expected output:

C =
     7
     8
     9
IA =
     4
     5
     3

Working with Empty Arrays

A = zeros(0, 3);
[C, IA, IC] = unique(A, 'rows')

Expected output:

C =
IA =
IC =

Using unique on GPU Arrays

G = gpuArray([5 3 5 1]);
[C, IA, IC] = unique(G, 'stable')

Expected output:

C =
     5
     3
     1
IA =
     1
     2
     4
IC =
     1
     2
     1
     3

Unique Characters in a Char Array

chars = ['m','z'; 'm','a'];
[C, IA] = unique(chars)

Expected output:

C =
    a
    m
    z
IA =
    4
    1
    3

Unique Strings with Row Deduplication

S = ["alpha" "beta"; "alpha" "beta"; "gamma" "beta"];
[C, IA, IC] = unique(S, 'rows', 'stable')

Expected output:

C =
  2x2 string array
    "alpha"    "beta"
    "gamma"    "beta"
IA =
     1
     3
IC =
     1
     1
     2

Using unique with coding agents

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

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

FAQ

Which ordering does unique use by default?⌄

Results are sorted in ascending order unless you pass 'stable', which preserves the first occurrence order.

How are the index outputs defined?⌄

IA indexes into the original data (or rows). IC is a column vector mapping each element (or row) of the input to the position of the corresponding unique value in C.

What do 'first' and 'last' control?⌄

They determine whether IA references the first or last occurrence of each distinct value/row. They do not affect C or IC.

Can I combine 'rows' with 'stable' or 'last'?⌄

Yes. All permutations of 'rows', 'stable'/'sorted', and 'first'/'last' are accepted. The runtime enforces MATLAB's validation rules.

Does unique support complex numbers or characters?⌄

Yes. Complex values use magnitude ordering for the sorted output, and character or string arrays produce results in their native container types (char arrays and string arrays respectively).

How does unique treat NaN values?⌄

All NaN values are considered equal. Sorted outputs place NaNs at the end; stable outputs keep their original relative order.

Are GPU arrays supported?⌄

Yes. When a provider lacks a native kernel, RunMat gathers GPU arrays to host memory and executes the host implementation, guaranteeing MATLAB-compatible output.

Does unique preserve array shape?⌄

Scalar outputs remain scalars. Otherwise, values are returned as column vectors (for element mode) or matrices with the same number of columns as the input (for 'rows').

What happens with empty inputs?⌄

Empty inputs (including empty matrices) return empty outputs with matching dimensions, and index outputs are empty column vectors.

Is unique stable?⌄

Sorting is stable where applicable; ties preserve their relative order. You can also request 'stable' explicitly.

Related Array functions

Sorting Sets

argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union

Grouping

accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply

Shape

blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat

Creation

colon · createArray · empty · eye · false · full · inf · linspace · logspace · magic · meshgrid · nan · nchoosek · ndgrid · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size

Open-source implementation

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

  • View the source for unique 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 unique works
  • Does RunMat run unique on the GPU?
  • Examples
  • Getting Sorted Unique Values
  • Preserving Input Order with 'stable'
  • Returning Indices for Reconstruction
  • Finding Unique Rows in a Matrix
  • Selecting Last Occurrences
  • Working with Empty Arrays
  • Using unique on GPU Arrays
  • Unique Characters in a Char Array
  • Unique Strings with Row Deduplication
  • Using unique with coding agents
  • FAQ
  • Related Array functions
  • Sorting Sets
  • Grouping
  • Shape
  • Creation
  • Indexing
  • Introspection
  • Open-source implementation
  • About RunMat