RunMat
GitHub

setdiff — Return values present in the first input but absent from the second, with MATLAB-compatible ordering modes.

setdiff(A, B) returns unique values (or rows) that appear in A but not in B. It supports MATLAB-compatible 'sorted' and 'stable' ordering modes plus row-wise set differences.

Syntax

C = setdiff(A, B)
C = setdiff(A, B, option...)
[C, ia] = setdiff(A, B)
[C, ia] = setdiff(A, B, option...)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesFirst input array.
BAnyYesSecond input array.
optionStringScalarVariadicOption tokens: 'rows'|'sorted'|'stable'.

Returns

NameTypeDescription
CAnyValues that appear in A but not in B.
iaNumericArrayIndices selecting retained values/rows from A.

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

Errors

IdentifierWhenMessage
RunMat:setdiff:LegacyOptionUnsupportedLegacy compatibility options are requested.setdiff: the 'legacy' behaviour is not supported
RunMat:setdiff:ConflictingOrderOptionsBoth 'sorted' and 'stable' options are provided.setdiff: cannot combine 'sorted' with 'stable'
RunMat:setdiff:UnknownOptionAn unsupported option token is provided.setdiff: unrecognised option

How setdiff works

  • setdiff(A, B) flattens inputs column-major, removes duplicates, subtracts the values of B from A, and returns the remaining elements sorted ascending by default.
  • [C, IA] = setdiff(A, B) also returns indices so that C = A(IA).
  • setdiff(A, B, 'stable') preserves the first appearance order from A instead of sorting.
  • setdiff(A, B, 'rows') treats each row as an element. Inputs must share the same number of columns.
  • Character arrays, string arrays, logical arrays, numeric types, and complex values are all supported.
  • Legacy flags ('legacy', 'R2012a') are not supported; RunMat always follows modern MATLAB semantics.

Does RunMat run setdiff on the GPU?

setdiff is registered as a residency sink. When tensors reside on the GPU and the active provider does not yet implement a setdiff hook, RunMat gathers them to host memory, performs the CPU implementation, and materialises host-resident results. Future providers can wire a custom hook to perform the set difference directly on-device without affecting existing callers.

Examples

Finding values exclusive to the first numeric vector

A = [5 7 5 1];
B = [7 1 3];
[C, IA] = setdiff(A, B)

Expected output:

C =
     5
IA =
     1

Preserving input order with 'stable'

A = [4 2 4 1 3];
B = [3 4 5 1];
[C, IA] = setdiff(A, B, 'stable')

Expected output:

C =
     2
IA =
     2

Working with rows of numeric matrices

A = [1 2; 3 4; 1 2];
B = [3 4; 5 6];
[C, IA] = setdiff(A, B, 'rows')

Expected output:

C =
     1     2
IA =
     1

Computing set difference for character data

A = ['m','z'; 'm','a'];
B = ['a','x'; 'm','a'];
[C, IA] = setdiff(A, B)

Expected output:

C =
    m
IA =
     1

Subtracting string arrays by row

A = ["alpha" "beta"; "gamma" "beta"];
B = ["gamma" "beta"; "delta" "beta"];
[C, IA] = setdiff(A, B, 'rows', 'stable')

Expected output:

C =
  1x2 string array
    "alpha"    "beta"
IA =
     1

Using setdiff with GPU arrays

G = gpuArray([10 4 6 4]);
H = gpuArray([6 4 2]);
C = setdiff(G, H)

Expected output:

C =
    10

Using setdiff with coding agents

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

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

FAQ

What ordering does setdiff use by default?

Results are sorted ascending. Specify 'stable' to preserve the first appearance order from the first input.

How are the index outputs defined?

IA points to the positions in A that correspond to each element (or row) returned in C, using MATLAB's one-based indexing.

Can I combine 'rows' with 'stable'?

Yes. 'rows' can be paired with either 'sorted' (default) or 'stable'. Other option combinations that conflict (e.g. 'sorted' with 'stable') are rejected.

Does setdiff remove NaN values from A when they exist in B?

Yes. NaN values are considered equal. If B contains NaN, all NaN entries from A are removed.

Are complex numbers supported?

Absolutely. Complex values use MATLAB's ordering rules (magnitude, then real part, then imaginary part) for the sorted output.

Does GPU execution change the results?

No. Until providers supply a device implementation, RunMat gathers GPU inputs and executes the CPU path to guarantee MATLAB-compatible behaviour.

What happens if the inputs have different classes?

RunMat follows MATLAB's rules: both inputs must share the same class (numeric/logical, complex, char, or string). Mixed-class inputs raise descriptive errors.

Can I request 'legacy' behaviour?

No. RunMat implements the modern semantics only. Passing 'legacy' or 'R2012a' results in an error.

Sorting Sets

argsort · intersect · ismember · issorted · sort · sortrows · union · unique

Shape

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

Creation

colon · eye · false · fill · linspace · logspace · magic · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

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

Open-source implementation

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

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.