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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | First input array. |
B | Any | Yes | — | Second input array. |
option | StringScalar | Variadic | — | Option tokens: 'rows'|'sorted'|'stable'. |
Returns
| Name | Type | Description |
|---|---|---|
C | Any | Values or rows that appear in exactly one input. |
ia | NumericArray | Indices selecting values or rows from A. |
ib | NumericArray | Indices selecting values or rows from B. |
Returned values from setxor depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:setxor:LegacyOptionUnsupported | Legacy compatibility options are requested. | setxor: the 'legacy' behaviour is not supported |
RunMat:setxor:ConflictingOrderOptions | Both 'sorted' and 'stable' options are provided. | setxor: cannot combine 'sorted' with 'stable' |
RunMat:setxor:UnknownOption | An unsupported option token is provided. | setxor: unrecognised option |
RunMat:setxor:RowsColumnMismatch | 'rows' mode is used and column counts differ. | setxor: inputs must have the same number of columns when using 'rows' |
RunMat:setxor:UnsupportedInputType | Input values cannot be converted into supported setxor domains. | setxor: unsupported input type |
RunMat:setxor:NumericClassMismatch | Numeric inputs have incompatible nondouble classes. | setxor: numeric inputs must have the same class, except double may be combined with one nondouble class |
RunMat:setxor:InvalidArgument | Option arguments are not string-like where required. | setxor: expected string option arguments |
RunMat:setxor:TooManyOutputs | More than three output arguments are requested. | setxor: too many output arguments |
RunMat:setxor:Internal | Internal conversion, allocation, or provider decode fails. | setxor: internal operation failed |
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 soCis a sorted or stable combination ofA(IA)andB(IB).setxor(A, B, 'stable')preserves first-seen order: exclusive values fromAappear first, followed by exclusive values fromB.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.
NaNvalues are treated as distinct, matching MATLABsetxorbehavior.- 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
1Preserving 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
3Symmetric 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 =
3NaN values remain distinct
C = setxor([5 NaN NaN], [5 NaN NaN])Expected output:
C =
NaN NaN NaN NaNUsing 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.
Related Array functions
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · sort · sortrows · union · unique
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
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how setxor is executed, line by line, in Rust.
- View the source for setxor 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.