# setxor

Set exclusive OR of two arrays

## Syntax

• `C = setxor(A,B)` example
• `C = setxor(A,B,'rows')`
• ```[C,ia,ib] = setxor(A,B)``` example
• ```[C,ia,ib] = setxor(A,B,'rows')``` example
• ```[C,ia,ib] = setxor(___,setOrder)``` example
• ```[C,ia,ib] = setxor(A,B,'legacy')``` example
• ```[C,ia,ib] = setxor(A,B,'rows','legacy')``` example

## Description

example

````C = setxor(A,B)` returns the data of `A` and `B` that are not in their intersection (the symmetric difference). If `A` and `B` are numeric arrays, logical arrays, character arrays, categorical arrays, datetime arrays, duration arrays, or cell arrays of strings, then `setxor` returns the values that occur in `A` or `B`, but not both. The values of `C` are in sorted order.If `A` and `B` are tables, then `setxor` returns the rows that occur in one or the other of the two tables, but not both. The rows of table `C` are in sorted order.```
````C = setxor(A,B,'rows')` treats each row of `A` and each row of `B` as single entities and returns the rows of matrices `A` and `B` that are not in their intersection. The rows of `C` are in sorted order.The `'rows'` option does not support cell arrays, unless one of the inputs is either a categorical array or a datetime array.```

example

``````[C,ia,ib] = setxor(A,B)``` also returns index vectors `ia` and `ib`.If `A` and `B` are numeric arrays, logical arrays, character arrays, categorical arrays, datetime arrays, duration arrays, or cell arrays of strings, then `C` is a sorted combination of the elements `A(ia)` and `B(ib)`.If `A` and `B` are tables, then `C` is a sorted combination of the rows of `A(ia,:)` and `B(ib,:)`.```

example

``````[C,ia,ib] = setxor(A,B,'rows')``` also returns index vectors `ia` and `ib`, such that `C` is a sorted combination of the rows of `A(ia,:)` and `B(ib,:)`.```

example

``````[C,ia,ib] = setxor(___,setOrder)``` returns `C` in a specific order using any of the input arguments in the previous syntaxes. `setOrder='sorted'` returns the values (or rows) of `C` in sorted order. `setOrder='stable'` returns the values (or rows) of `C` in the same order as `A` and `B`. If no value is specified, the default is `'sorted'`.```

example

``````[C,ia,ib] = setxor(A,B,'legacy')``` and ```[C,ia,ib] = setxor(A,B,'rows','legacy')``` preserve the behavior of the `setxor` function from R2012b and prior releases.The `'legacy'` option does not support categorical arrays, tables, datetime arrays, or duration arrays.```

## Examples

collapse all

### Symmetric Difference of Two Vectors

Define two vectors with a value in common.

`A = [5 1 3 3 3]; B = [4 1 2];`

Find the values of `A` and `B` that are not in their intersection.

`C = setxor(A,B)`
```C = 2 3 4 5```

### Symmetric Difference of Two Tables

Define two tables with rows in common.

```A = table([1:5]',['A';'B';'C';'D';'E'],logical([0;1;0;1;0])) B = table([1:2:10]',['A';'C';'E';'G';'I'],logical(zeros(5,1)))```
```A = Var1 Var2 Var3 ---- ---- ----- 1 A false 2 B true 3 C false 4 D true 5 E false B = Var1 Var2 Var3 ---- ---- ----- 1 A false 3 C false 5 E false 7 G false 9 I false```

Find the rows of `A` and `B` that are not in their intersection.

`C = setxor(A,B)`
```C = Var1 Var2 Var3 ---- ---- ----- 2 B true 4 D true 7 G false 9 I false```

### Symmetric Difference of Two Vectors and Indices to Different Values

Define two vectors with a value in common.

`A = [5 1 3 3 3]; B = [4 1 2];`

Find the values of `A` and `B` that are not in their intersection as well as the index vectors `ia` and `ib`.

`[C,ia,ib] = setxor(A,B)`
```C = 2 3 4 5 ia = 3 1 ib = 3 1```

`C` is a sorted combination of the elements `A(ia)` and `B(ib)`.

### Symmetric Difference of Two Tables and Indices to Different Rows

Define a table, `A`, of gender, age, and height for five people.

```A = table(['M';'M';'F'],[27;52;31],[74;68;64],... 'VariableNames',{'Gender' 'Age' 'Height'},... 'RowNames',{'Ted' 'Fred' 'Betty'})```
```A = Gender Age Height ------ --- ------ Ted M 27 74 Fred M 52 68 Betty F 31 64 ```

Define a table, `B`, with the same variables as `A`.

```B = table(['F';'M'],[64;68],[31;47],... 'VariableNames',{'Gender' 'Height' 'Age'},... 'RowNames',{'Meg' 'Joe'})```
```B = Gender Height Age ------ ------ --- Meg F 64 31 Joe M 68 47 ```

Find the rows of `A` and `B` that are not in their intersection, as well as the index vectors `ia` and `ib`.

`[C,ia,ib] = setxor(A,B)`
```C = Gender Age Height ------ --- ------ Ted M 27 74 Joe M 47 68 Fred M 52 68 ia = 1 2 ib = 2```

`C` is a sorted combination of the elements `A(ia,:)` and `B(ib,:)`.

### Symmetric Difference of Rows in Two Matrices

Define two matrices with rows in common.

```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];```

Find the rows of `A` and `B` that are not in their intersection as well as the index vectors `ia` and `ib`.

`[C,ia,ib] = setxor(A,B,'rows')`
```C = 7 7 1 7 7 2 7 8 9 ia = 2 1 ib = 3```

`C` is a sorted combination of the rows of `A(ia,:)` and `B(ib,:)`.

### Symmetric Difference of Two Vectors in Specified Order

Use the `setOrder` argument to specify the ordering of the values in `C`.

Specify `'stable'` if you want the values in `C` to have the same order as `A` and `B`.

```A = [5 1 3 3 3]; B = [4 1 2]; [C,ia,ib] = setxor(A,B,'stable')```
```C = 5 3 4 2 ia = 1 3 ib = 1 3```

Alternatively, you can specify `'sorted'` order.

`[C,ia,ib] = setxor(A,B,'sorted')`
```C = 2 3 4 5 ia = 3 1 ib = 3 1```

### Symmetric Difference of Vectors Containing NaNs

Define two vectors containing `NaN`.

`A = [5 NaN NaN]; B = [5 NaN NaN];`

Find the symmetric difference of vectors `A` and `B`.

`C = setxor(A,B)`
```C = NaN NaN NaN NaN```

The `setxor` function treats `NaN` values as distinct.

### Cell Array of Strings with Trailing White Space

Create a cell array of strings, `A`.

```A = {'dog','cat','fish','horse'}; ```

Create a cell array of strings, `B`, where some of the strings have trailing white space.

`B = {'dog ','cat','fish ','horse'};`

Find the strings that are not in the intersection of `A` and `B`.

`[C,ia,ib] = setxor(A,B)`
```C = 'dog' 'dog ' 'fish' 'fish ' ia = 1 3 ib = 1 3```

`setxor` treats trailing white space in cell arrays of strings as distinct characters.

### Symmetric Difference of Vectors of Different Classes and Shapes

Create a column vector character array.

`A = ['A';'B';'C'], class(A)`
```A = A B C ans = char ```

Create a row vector containing elements of numeric type `double`.

`B = [66 67 68], class(B)`
```B = 66 67 68 ans = double ```

Find the symmetric difference of `A` and `B`.

`C = setxor(A,B)`
```C = A D ```

The result is a column vector character array.

`class(C)`
```ans = char```

### Symmetric Difference of Char and Cell Array of Strings

Create a character array, `A`.

```A = ['cat';'dog';'fox';'pig']; class(A)```
```ans = char```

Create a cell array of strings, `B`.

```B={'dog','cat','fish','horse'}; class(B)```
```ans = cell```

Find the strings that are not in the intersection of `A` and `B`.

`C = setxor(A,B)`
```C = 'fish' 'fox' 'horse' 'pig'```

The result, `C`, is a cell array of strings.

`class(C)`
```ans = cell```

### Preserve Legacy Behavior of setxor

Use the `'legacy'` flag to preserve the behavior of `setxor` from R2012b and prior releases in your code.

Find the symmetric difference of `A` and `B` with the current behavior.

```A = [5 1 3 3 3]; B = [4 1 2 2]; [C1,ia1,ib1] = setxor(A,B)```
```C1 = 2 3 4 5 ia1 = 3 1 ib1 = 3 1```

Find the symmetric difference and preserve the legacy behavior.

`[C2,ia2,ib2] = setxor(A,B,'legacy')`
```C2 = 2 3 4 5 ia2 = 5 1 ib2 = 4 1```

## Input Arguments

collapse all

### `A,B` — Input arraysnumeric arrays | logical arrays | character arrays | categorical arrays | datetime arrays | duration arrays | cell arrays of strings | tables

Input arrays, specified as numeric arrays, logical arrays, character arrays, categorical arrays, datetime arrays, duration arrays, cell arrays of strings, or tables.

`A` and `B` must belong to the same class with the following exceptions:

• `logical`, `char`, and all numeric classes can combine with `double` arrays.

• Cell arrays of strings can combine with `char` arrays.

• Categorical arrays can combine with cell arrays of strings or single strings.

• Datetime arrays can combine with cell arrays of date strings or single date strings.

If `A` and `B` are both ordinal categorical arrays, they must have the same sets of categories, including their order. If neither `A` nor `B` are ordinal, they need not have the same sets of categories, and the comparison is performed using the category names. In this case, the categories of `C` are the sorted union of the categories from `A` and `B`.

If you specify the `'rows'` option, `A` and `B` must have the same number of columns.

If `A` and `B` are tables, they must have the same variable names. Conversely, the row names do not matter. Two rows that have the same values, but different names, are considered equal.

If `A` and `B` are datetime arrays, they must be consistent with each other in whether they specify a time zone.

Furthermore, `A` and `B` can be objects with the following class methods:

• `sort` (or `sortrows` for the `'rows'` option)

• `eq`

• `ne`

The object class methods must be consistent with each other. These objects include heterogeneous arrays derived from the same root class.

### `setOrder` — Order flag`'sorted'` (default) | `'stable'`

Order flag, specified as `'sorted'` or `'stable'`, indicates the order of the values (or rows) in `C`.

Order FlagMeaning
`'sorted'`The values (or rows) in `C` return in sorted order. For example: ```C = setxor([5 1 3],[4 1 2],'sorted')``` returns ```C = [2 3 4 5]```.
`'stable'`The values (or rows) in `C` return in the same order as in `A` and `B`. For example: `C = setxor([5 1 3],[4 1 2],'stable')` returns `C = [5 3 4 2]`.

## Output Arguments

collapse all

### `C` — Symmetric difference arrayvector | matrix | table

Symmetric difference array, returned as a vector, matrix, or table. If the inputs `A` and `B` are tables, the order of the variables in the resulting table, `C`, is the same as the order of the variables in `A`.

The following describes the shape of `C` when the inputs are vector or matrices and when the `'legacy'` flag is not specified:

• If the `'rows'` flag is not specified, then C is a column vector unless both `A` and `B` are row vectors.

• If the `'rows'` flag is not specified and both `A` and `B` are row vectors, then `C` is a row vector.

• If the`'rows'` flag is specified, then `C` is a matrix containing the rows of `A` and `B` that are not in the intersection.

• If all the values (or rows) of `A` are also in `B`, then `C` is an empty matrix.

The class of the inputs `A` and `B` determines the class of `C`:

• If the class of `A` and `B` are the same, then `C` is the same class.

• If you combine a `char` or nondouble numeric class with `double`, then `C` is the same class as the nondouble input.

• If you combine a `logical` class with `double`, then `C` is `double`.

• If you combine a cell array of strings with `char`, then `C` is a cell array of strings.

• If you combine a categorical array with a cell array of strings or single string, then `C` is a categorical array.

• If you combine a datetime array with a cell array of date strings or single date string, then `C` is a datetime array.

### `ia` — Index to `A`column vector

Index to `A`, returned as a column vector when the `'legacy'` flag is not specified. `ia` identifies the values (or rows) in `A` that contribute to the symmetric difference. If there is a repeated value (or row) appearing exclusively in `A`, then `ia` contains the index to the first occurrence of the value (or row).

### `ib` — Index to `B`column vector

Index to `B`, returned as a column vector when the `'legacy'` flag is not specified. `ib` identifies the values (or rows) in `B` that contribute to the symmetric difference. If there is a repeated value (or row) appearing exclusively in `B`, then `ib` contains the index to the first occurrence of the value (or row).

collapse all

### Tips

• To find the symmetric difference with respect to a subset of variables from a table, you can use column subscripting. For example, you can use `setxor(A(:,vars),B(:,vars))`, where `vars` is a positive integer, a vector of positive integers, a variable name, a cell array of variable names, or a logical vector.