| Contents | Index |
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.
[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'.
A = [3 6 2 1 5 1 1]; B = [2 4 6]; C = setdiff(A,B)
C =
1 3 5A = [3 6 2 1 5 1 1]; B = [2 4 6]; [C,ia] = setdiff(A,B)
C =
1 3 5
ia =
7 1 5A = [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
3Use 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
5Alternatively, 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.
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 5Find 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
5Notice 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 5C3 and ia3 match C1 and ia1.
A = [5 NaN NaN]; B = [5 NaN]; C = setdiff(A,B)
C = NaN NaN
setdiff treats NaN values as distinct.
C | Difference of A and B 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
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. |
intersect | ismember | issorted | setxor | sort | union | unique

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |