Set exclusive OR of two arrays (2024)

Set exclusive OR of two arrays

collapse all in page

Syntax

C = setxor(A,B)

C = setxor(A,B,setOrder)

C = setxor(A,B,___,'rows')

C = setxor(A,B,'rows',___)

[C,ia,ib]= setxor(___)

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

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

Description

example

C = setxor(A,B) 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 in A or B, but not both. C is in sorted order.

  • If A and B are tables or timetables, then setxor returns the rows that occur in one or the other of the two tables, but not both. For timetables, setxor takes row times into account to determine equality, and sorts the output timetable C by row times.

example

C = setxor(A,B,setOrder) returns C in a specific order. setOrder can be 'sorted' or 'stable'.

C = setxor(A,B,___,'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 and B 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.

example

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

  • Generally, the values in C are a sorted combination of the elements of A(ia) and B(ib).

  • If the 'rows' option is specified, then C is a sorted combination of the rows of A(ia,:) and B(ib,:).

  • If A and B are tables or timetables, then C is a sorted combination of the rows of A(ia,:) and B(ib,:).

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 categoricalarrays, datetime arrays, duration arrays, tables, or timetables.

Examples

collapse all

Symmetric Difference of Two Vectors

Open Live Script

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 = 1×4 2 3 4 5

Symmetric Difference of Two Tables

Open Live Script

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 = setxor(A,B)
C=4×3 table 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

Open Live Script

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 = 1×4 2 3 4 5
ia = 2×1 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

Open Live Script

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,ia,ib] = setxor(A,B)
C=3×3 table Gender Age Height ______ ___ ______ Ted M 27 74 Joe M 47 68 Fred M 52 68 
ia = 2×1 1 2
ib = 2

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

Symmetric Difference of Rows in Two Matrices

Open Live Script

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
ia = 2×1 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

Open Live Script

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 = 1×4 5 3 4 2
ia = 2×1 1 3
ib = 2×1 1 3

Alternatively, you can specify 'sorted' order.

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

Symmetric Difference of Vectors Containing NaNs

Open Live Script

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 = 1×4 NaN NaN NaN NaN

The setxor function treats NaN values as distinct.

Cell Array of Character Vectors with Trailing White Space

Open Live Script

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,ia,ib] = setxor(A,B)
C = 1x4 cell {'dog'} {'dog '} {'fish'} {'fish '}
ia = 2×1 1 3
ib = 2×1 1 3

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

Symmetric Difference of Vectors of Different Classes and Shapes

Open Live Script

Create a column vector character array.

A = ['A';'B';'C'], class(A)
A = 3x1 char array 'A' 'B' 'C'
ans = 'char'

Create a row vector containing elements of numeric type double.

B = [66 67 68], class(B)
B = 1×3 66 67 68
ans = 'double'

Find the symmetric difference of A and B.

C = setxor(A,B)
C = 2x1 char array 'A' 'D'

The result is a column vector character array.

class(C)
ans = 'char'

Symmetric Difference of Char and Cell Array of Character Vectors

Open Live Script

Create a character vector, A.

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

Create a cell array of character vectors, B.

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

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

C = setxor(A,B)
C = 4x1 cell {'fish' } {'fox' } {'horse'} {'pig' }

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

class(C)
ans = 'cell'

Preserve Legacy Behavior of setxor

Open Live Script

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 = 1×4 2 3 4 5
ia1 = 2×1 3 1
ib1 = 2×1 3 1

Find the symmetric difference and preserve the legacy behavior.

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

Input Arguments

collapse all

setOrderOrder flag
'sorted' (default) | 'stable'

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

FlagDescription
'sorted'

The values (or rows) in C return in sorted order as returned by sort.

Example

C = 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 in A, then B.

Example

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

Data Types: char | string

Output Arguments

collapse all

C — Symmetric difference array
vector | matrix | table | timetable

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 in C is the same as the order of the variables in A.

The following describes the shape of C when the inputs are vectors 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, in which case C is a row vector. For example, setxor([],[1 2]) returns a column 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 empty.

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

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

  • If you combine a char or nondoublenumeric class with double, then C isthe same class as the nondouble input.

  • If you combine a logical classwith double, then C is double.

  • If you combine a cell array of character vectors with char,then C is a cell array of character vectors.

  • If you combine a categorical array with a charactervector, cell array of character vectors, or string, then C isa categorical array.

  • If you combine a datetime array with a cell arrayof date character vectors or single date character vector, then C isa datetime array.

  • If you combine a string array with a character vectoror cell array of character vectors, then C is astring 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).

Tips

  • To find the symmetric difference with respect to a subset of variables from a table or timetable, 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. Alternatively, you can use vartype to create a subscript that selects variables of a specified type.

Extended Capabilities

Version History

Introduced before R2006a

See Also

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

Topics

  • Combine Categorical Arrays

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Set exclusive OR of two arrays (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Set exclusive OR of two arrays (2024)

FAQs

How to equate two arrays in Java? ›

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

How to check if two arrays are in the same order? ›

Typically we solve these sorts of problems by sorting the arrays first.
  1. Sort both arrays using any strategy that makes sense.
  2. Iterate over one array, checking the corresponding position in the other array for equality as you go. ...
  3. If arr1[i] !== arr2[i] return false.
  4. If you exit the loop, return true.
May 3, 2023

How to check if two arrays contain the same elements? ›

This approach will be as follows:
  1. Find XOR of all elements of first Array as XOR(Arr1)
  2. Then XOR each element of second Array with this.
  3. If the result is 0, then the arrays are equal, otherwise not.
Jul 5, 2024

How do you find unique elements in two arrays? ›

Using for loop
  1. Create an empty array that would store the unique elements from the two arrays.
  2. Iterate over all elements of array1 using a loop.
  3. Set the initial flag value as 0 for each iteration.
  4. In that loop Iterate over all elements of array2 using another loop and check if array1[element] is present in array2.
May 16, 2024

Can you equate two arrays? ›

In other words, two arrays are equal if they contain equivalent elements in the same order. To test for reference equality, use the reference equality operators, == and != .

How to set two arrays equal to each other Java? ›

In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null. Arrays class in java provide the method Arrays. equals() to check whether two arrays are equal or not.

How to compare two arrays? ›

Two arrays object reference e1 and e2 are deeply equal if they hold any of the following condition:
  1. e1=e2.
  2. equals(e2) returns true.
  3. If e1 and e2 are both the same primitive type the overloading of the method Arrays. equals(e1, e2) returns true.
  4. If e1 and e2 are both arrays of object reference types, the method Arrays.

How do I sort two arrays in the same order? ›

Method 1 (first Concatenate then Sort): In this case, we first append the two unsorted lists. Then we simply sort the concatenated list.

What is the fastest way to check if two arrays are equal? ›

The simplest and fastest way to compare two arrays is to convert them both to strings using the JSON. stringify() method and a comparison operator ( === ). This approach is great for really basic instances when you just need to check if a simple array of items is equal to another.

How do you check if two arrays of objects are equal? ›

isEqual() method to compare two arrays of objects. This method performs a deep comparison between two values to determine if they are equivalent.

How do you find the missing element between two arrays? ›

To determine whether an array has every element in another array we need to follow these simple steps: first sort both the given arrays, second iterate over both the arrays simultaneously, lastly at a given iteration if the value of both the iterators are different, return the element from the first array, as it is the ...

How do you check array for same values? ›

In this approach, we use the Array. some() method to determine if any element in the array does not match the first element. If the method returns false, it means all elements are equal. Knowing about to check if all values in an array are equal enhances your capability to manage and validate data effectively.

How do you compare two array list elements? ›

Java provides a method for comparing two Array List. The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal.

How do you check if an array has unique values? ›

Using `Array. filter()` with a callback that checks if an element's index is equal to its first occurrence's index in the array (`Array. indexOf()`), the function filters out duplicate elements. The resulting array's length equals the original if it contains only unique values.

How do you find the number of common elements in two arrays? ›

Using Iterative Methods
  1. Get the two java Arrays.
  2. Iterate through each and every element of the arrays one by one and check whether they are common in both.
  3. Add each common element in the set for unique entries.
Mar 6, 2024

How do you equate one array to another array? ›

Ensure that the two arrays have the same rank (number of dimensions) and compatible element data types. Use a standard assignment statement to assign the source array to the destination array. Do not follow either array name with parentheses.

How do you check if two array lists are equal in Java? ›

Java equals() method

This method accepts an object to be compared for equality with the list. It returns true if the specified object is equal to the list, else returns false. In the following example, we have create two ArrayList firstList and secondList. Comparing both list by using equals() method, it returns true.

How to check if 2 arrays of objects are equal in javascript? ›

Methods to compare two arrays in Javascript are:
  1. Equality comparison: Using the == or === operators.
  2. JSON. stringify(): Converting the arrays to JSON strings, and then comparing those.
  3. For Loop: traversing both arrays using a for loop and comparing each element.
  4. Array. prototype. ...
  5. Array. prototype.
Jan 9, 2024

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6090

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.