setxor - Exclusive OR of two sets of data - MATLAB (original) (raw)

Exclusive OR of two sets of data

Syntax

Description

[C](#btcnv3n-1-C) = setxor([A,B](#btcnv3n-1%5Fsep%5Fshared-AB)) returns the data of A and B that are not in their intersection (the symmetric difference), with no repetitions. That is,setxor returns the data that occurs inA or B, but not both.C is in sorted order.

example

[C](#btcnv3n-1-C) = setxor([A,B](#btcnv3n-1%5Fsep%5Fshared-AB),[setOrder](#btcnv3n-1-setOrder)) returns C in a specific order. setOrder can be 'sorted' or 'stable'.

example

[C](#btcnv3n-1-C) = setxor([A,B](#btcnv3n-1%5Fsep%5Fshared-AB),___,'rows') and`C` = setxor(`A,B`,'rows',___) treat each row of A and each row of B as single entities and returns the rows of A andB that are not in their intersection, with no repetitions. You must specify A and B and optionally can specify setOrder.

The 'rows' option does not support cell arrays, unless one of the inputs is either a categorical array or a datetime array.

[[C](#btcnv3n-1-C),[ia](#btcnv3n-1-ia),[ib](#btcnv3n-1-ib)] = setxor(___) also returns index vectors ia and ib using any of the previous syntaxes.

example

[[C](#btcnv3n-1-C),[ia](#btcnv3n-1-ia),[ib](#btcnv3n-1-ib)] = setxor([A,B](#btcnv3n-1%5Fsep%5Fshared-AB),'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, datetime arrays, duration arrays, tables, or timetables.

example

Examples

collapse all

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.

Define two tables with rows in common.

A = table([1:5]',['A';'B';'C';'D';'E'],logical([0;1;0;1;0]))

A=5×3 table Var1 Var2 Var3 ____ ____ _____

 1       A      false
 2       B      true 
 3       C      false
 4       D      true 
 5       E      false

B = table([1:2:10]',['A';'C';'E';'G';'I'],logical(zeros(5,1)))

B=5×3 table 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=4×3 table Var1 Var2 Var3 ____ ____ _____

 2       B      true 
 4       D      true 
 7       G      false
 9       I      false

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 is a sorted combination of the elements A(ia) and B(ib).

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=3×3 table 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=2×3 table 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=3×3 table Gender Age Height ______ ___ ______

Ted       M       27       74  
Joe       M       47       68  
Fred      M       52       68  

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

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 = 3×3

 7     7     1
 7     7     2
 7     8     9

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

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

Alternatively, you can specify 'sorted' order.

[C,ia,ib] = setxor(A,B,'sorted')

Define two vectors containing NaN.

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

Find the symmetric difference of vectors A and B.

The setxor function treats NaN values as distinct.

Create a cell array of character vectors, A.

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

Create a cell array of character vectors, B, where some of the vectors have trailing white space.

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

Find the character vectors that are not in the intersection of A and B.

C = 1×4 cell {'dog'} {'dog '} {'fish'} {'fish '}

setxor treats trailing white space in cell arrays of character vectors as distinct characters.

Create a column vector character array.

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

A = 3×1 char array 'A' 'B' 'C'

Create a row vector containing elements of numeric type double.

Find the symmetric difference of A and B.

C = 2×1 char array 'A' 'D'

The result is a column vector character array.

Create a character vector, A.

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

Create a cell array of character vectors, B.

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

Find the character vectors that are not in the intersection of A and B.

C = 4×1 cell {'fish' } {'fox' } {'horse'} {'pig' }

The result, C, is a cell array of character vectors.

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)

Find the symmetric difference and preserve the legacy behavior.

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

Input Arguments

collapse all

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

Flag Description
'sorted' The values (or rows) in C return in sorted order as returned by sort.ExampleC = setxor([5 1 3],[4 1 2],'sorted')C = 2 3 4 5
'stable' The values (or rows) in C return in the same order as they appear inA, thenB.ExampleC = setxor([5 1 3],[4 1 2],'stable')C = 5 3 4 2

Data Types: char | string

Output Arguments

collapse all

Symmetric difference array, returned as a vector, matrix, table, or timetable. If the inputs A and B are tables or timetables, then the order of the variables inC is the same as the order of the variables inA.

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

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

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).

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).

Tips

Extended Capabilities

expand all

Thesetxor function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

Usage notes and limitations:

The setxor function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a