Skip to Main Content Skip to Search
Product Documentation

setdiff - Find set difference of two arrays

In a future release, the behavior of setdiff 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 setdiff. For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.

Syntax

  • C = setdiff(A,B) example
  • C = setdiff(A,B,'rows')
  • [C,ia] = setdiff(A,B) example
  • [C,ia] = setdiff(A,B,'rows') example

  • [C,ia] = setdiff(___,'R2012a') example
  • [C,ia] = setdiff(___,'legacy') example

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

Description

example

C = setdiff(A,B) values in A that are not in B. The values of C are in sorted order.

C = setdiff(A,B,'rows') returns the rows from A that are not in B. The rows of matrix C are in sorted order. The rows of the matrix C are in sorted order.

example

[C,ia] = setdiff(A,B) also returns the index vector ia, such that C = A(ia).

example

[C,ia] = setdiff(A,B,'rows') also returns index vectors ia, such that C = A(ia,:).

example

[C,ia] = setdiff(___,'R2012a') adopts the future behavior of the setdiff function. [C,ia] = setdiff(___,'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] = setdiff(A,B,setOrder) and [C,ia] = setdiff(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.

Examples

Difference of Two Vectors

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

     1     3     5

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

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

     1     3     5

ia =

     7     1     5

Difference of the Rows in Two Matrices

A = [7 9 7; 0 0 0; 7 9 7; 5 5 5; 1 4 5];
B = [0 0 0; 5 5 5];
[C,ia] = setdiff(A,B,'rows')
C =

     1     4     5
     7     9     7

ia =

     5
     3

Difference of Two Vectors with Specified Output Order

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

Specify 'stable' or 'sorted' when the order of the values in C are important.

A = [3 6 2 1 5 1 1]; B = [2 4 6];
[C,ia] = setdiff(A,B,'stable')
C =

     3     1     5

ia =

     1
     4
     5

Alternatively, you can specify 'sorted' order.

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

     1     3     5

ia =

     4
     1
     5

setdiff 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 and ia conforms to the current behavior.

Assessing the Impact of the Forthcoming Changes to setdiff

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 difference of A and B with the current default behavior.

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

     1     3     5

ia1 =

     7     1     5

Find the difference of A and B, and adopt the forthcoming behavior. In the future, this behavior will be the default.

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

     1     3     5

ia2 =

     4
     1
     5

Notice that ia1 and ia2 have different shapes and content.

Find the difference of A and B, and preserve the current behavior.

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

     1     3     5

ia3 =

     7     1     5

C3 and ia3 match C1 and ia1.

Find the Difference of Vectors Containing NaNs

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

   NaN   NaN

setdiff treats NaN values as distinct.

Input Arguments

A,B

Input Arrays
Vectors | Matrices | N-D Arrays

Input arrays can be logical, char, a cell array of strings, or any numeric class. A and B also can 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, setdiff returns the rows from A that are not in B. The arrays, A and B, must have the same number of columns when you use the rows flag. 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 = setdiff([4 1 3 2],[2 1],'sorted') returns C = [3 4].
'stable'The values (or rows) in C, are returned in the same order as in A. For example: C = setdiff([4 1 3 2],[2 1],'stable') returns C = [4 3].

'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

Difference of A and B
Vector | Matrix

Difference of A and B 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 that are not in B.

ia

Index to A
Vector

Index to A identifies the values (or rows) in A that are not in B. 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 setdiff with the setOrder or 'R2012a' argument, ia contains the index to the first occurrence of any repeated value (or row) in A.

See Also

intersect | ismember | issorted | setxor | 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