setxor — Return values or rows that appear in exactly one of two inputs.

setxor(A, B) returns the symmetric difference of two arrays: values or rows that occur in A or B, but not both. Results are sorted by default and can be returned in first-seen order with 'stable'.

Syntax

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

Inputs

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

Returns

NameTypeDescription
CAnyValues or rows that appear in exactly one input.
iaNumericArrayIndices selecting values or rows from A.
ibNumericArrayIndices selecting values or rows from B.

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

Errors

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

How setxor works

  • setxor(A, B) flattens inputs column-major, removes duplicate non-NaN values within each input, and returns the sorted symmetric difference. Element outputs are row vectors when both inputs are row vectors; otherwise they are column vectors.
  • [C, IA, IB] = setxor(A, B) returns one-based indices so C is a sorted or stable combination of A(IA) and B(IB).
  • setxor(A, B, 'stable') preserves first-seen order: exclusive values from A appear first, followed by exclusive values from B.
  • setxor(A, B, 'rows') treats each row as one set element. Numeric, complex, character, and string row inputs must have matching column counts.
  • Numeric outputs preserve matching input classes. A double input may be combined with one nondouble numeric class, in which case the nondouble class is preserved; two different nondouble classes are rejected.
  • Character arrays can be compared with numeric character-code arrays. String arrays can be compared with character vectors by treating each character row as a string scalar.
  • NaN values are treated as distinct, matching MATLAB setxor behavior.
  • Legacy flags ('legacy', 'R2012a') and requests for more than three outputs are rejected; RunMat implements modern MATLAB semantics.

Does RunMat run setxor on the GPU?

setxor is registered as a residency sink. Host execution is authoritative today, and GPU inputs are gathered before set semantics are evaluated.

Examples

Symmetric difference of two numeric vectors

A = [5 1 3 3 3];
B = [4 1 2];
[C, IA, IB] = setxor(A, B)

Expected output:

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

Preserving input order

[C, IA, IB] = setxor([5 1 3 3 3], [4 1 2], 'stable')

Expected output:

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

Symmetric difference of rows

A = [7 8 9; 7 7 1; 7 7 1; 1 2 3; 4 5 6];
B = [1 2 3; 4 5 6; 7 7 2];
[C, IA, IB] = setxor(A, B, 'rows')

Expected output:

C =
     7     7     1
     7     7     2
     7     8     9
IA =
     2
     1
IB =
     3

NaN values remain distinct

C = setxor([5 NaN NaN], [5 NaN NaN])

Expected output:

C =
   NaN   NaN   NaN   NaN

Using setxor with coding agents

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

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

FAQ

What ordering does setxor use by default?

Results are sorted ascending by default. Specify 'stable' to preserve the first occurrence order from A followed by B.

How are IA and IB defined?

IA indexes exclusive values or rows from A, and IB indexes exclusive values or rows from B. Both use MATLAB's one-based indexing.

Can I use setxor with rows?

Yes. Use 'rows' for numeric, complex, character, or string matrices with matching column counts.

How are NaN values handled?

Unlike several other set helpers, MATLAB setxor treats NaN values as distinct. RunMat preserves that behavior.

Does GPU execution change results?

No. GPU tensors are gathered to host memory and evaluated by the same CPU implementation used for ordinary tensors.

Shape

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

Creation

colon · eye · false · fill · 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 setxor 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.