Skip to Main Content Skip to Search
Product Documentation

setxor - Find set exclusive OR of two arrays

In a future release, the behavior of setxor will change. This change is introduced for adoption in R2012a. For a detailed demonstration that shows how you can preview the future behavior and preserve the current behavior of your existing code, see the example, Assessing the Impact of the Forthcoming Changes to setxor . For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.

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(___,'R2012a') example
  • [C,ia,ib] = setxor(___,'legacy') example

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

Description

example

C = setxor(A,B) returns the values of A and B that are not in their intersection (the symmetric difference). The values of C are in sorted order.

C = setxor(A,B,'rows') returns the rows of matrices A and B that are not in their intersection. The rows of the matrix C are in sorted order.

example

[C,ia,ib] = setxor(A,B) also returns index vectors ia and ib, such that C is a sorted combination of the elements 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 elements A(ia,:) and B(ib,:).

example

[C,ia,ib] = setxor(___,'R2012a') adopts the future behavior of the setxor function. [C,ia,ib] = setxor(___,'legacy') preserves the current behavior. The ___ symbol signifies that you can specify either flag, 'R2012a' or 'legacy', as the final argument with any previous syntax that accepts A, B, or 'rows'.

example

[C,ia,ib] = setxor(A,B,setOrder) and [C,ia,ib] = setxor(A,B,'rows',setOrder) returns C in a specific order. 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.

Examples

Symmetric Difference of Two Vectors

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

     2     3     4     5

Find Symmetric Difference of Two Vectors and the Indices to the Different Values

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

     2     3     4     5

ia =

     5     1

ib =

     3     1

Symmetric Difference of the Rows in Two Matrices

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')
C =

     7     7     1
     7     7     2
     7     8     9

ia =

     3
     1

ib =

     3

Symmetric Difference of Two Vectors in a 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

setxor adopts the forthcoming behavior when you specify the setOrder argument. If you omit the setOrder argument, the output is sorted, but the size and content of C, ia, and ib conforms to the current behavior.

Assessing the Impact of the Forthcoming Changes to setxor

Use the 'R2012a' flag to assess the impact of the forthcoming behavior changes. Use the 'legacy' flag to preserve the current behavior of your existing code.

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

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

     2     3     4     5

ia1 =

     5     1


ib1 =

     4     1

Find the symmetric difference and opt into the forthcoming behavior. In the future, this behavior will be the default.

[C2,ia2,ib2] = setxor(A,B,'R2012a')
C2 =

     2     3     4     5

ia2 =

     3
     1

ib2 =

     3
     1

Notice that ia1 and ia2 have different shapes and content. This also true for ib1 and ib2.

Find the symmetric difference and preserve the current behavior.

[C3,ia3,ib3] = setxor(A,B,'legacy')
C3 =

     2     3     4     5

ia3 =

     5     1

ib3 =

     4     1

Notice that C3, ia3, and ic3 match C1, ia1, and ic1 respectively.

Find Symmetric Difference of Vectors Containing NaNs

A = [5 NaN NaN]; B = [5 NaN NaN];
C = setxor(A,B)
C =

   NaN   NaN   NaN   NaN

The setxor function treats NaN values as distinct.

Input Arguments

A,B

Input Arrays
Vectors | Matrices | N-D Arrays

Input arrays of which to find the symmetric difference. A and B can be logical, char, a cell array of strings, or any numeric class. A and B can also be any object with the class methods: sort (or sortrows for the 'rows' option), eq, and ne. This includes heterogeneous arrays derived from the same root class.

A and B must be of 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.

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Support: Yes

'rows'

Rows Flag

Rows flag, specified as 'rows', identifies the rows of A and B as individual elements. When you specify this flag, setxor returns the rows of A andB that are not in their intersection. A and B must have the same number of columns. This option does not support cell arrays.

setOrder

Order Flag
'sorted' | '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, are returned 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, are returned 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].

'legacy'

Legacy Behavior Flag

Legacy behavior flag, when specified, preserves the current behavior. If the new behavior adversely affects your existing code, you can preserve the current behavior by specifying 'legacy' as the final argument.

'R2012a'

Future Behavior Flag

Future behavior flag, when specified, adopts the future behavior change. Specify R2012a' as the final argument to see what impact the new behavior has on your existing code.

Output Arguments

C

Symmetric Difference Array
Vector | Matrix

Symmetric difference array is returned as a vector or matrix. C is a vector, unless you specify the 'rows' flag. If 'rows' is specified, C is a matrix containing the rows of A and B that are not in their intersection.

ia

Index to A
Vector

Index to A 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 last occurrence of the value (or row).

If you call setxor with the setOrder or 'R2012a' argument, ia contains the index to the first occurrence of any repeated value (or row) in A.

ib

Index to B
Vector

Index to B 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 last occurrence of the value (or row).

If you call setxor with the setOrder or 'R2012a' argument, ib contains the index to the first occurrence of any repeated value (or row) in B.

See Also

intersect | ismember | issorted | setdiff | sort | union | unique

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS