Syntax

Description

C = setdiff(A,B) returns
the data in A that is not in B, with no repetitions.
C is in sorted order.

If A and B are tables or timetables, then
setdiff returns the rows from A that are not in
B. For timetables, setdiff takes row times into
account to determine equality, and sorts the output timetable C by row
times.

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

C = setdiff(A,B___,'rows')
and
C = setdiff(A,B,'rows',___)
treat each row of A and each row of B as single entities
and return the rows from A that are not in B, 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.

Input arrays, specified as numeric arrays, logical arrays, character
arrays, string arrays, categorical arrays, datetime arrays, duration
arrays, cell arrays of character vectors, tables, or timetables. If
you specify the 'rows' option, A and B must
have the same number of columns.

A and B must belong to
the same class with the following exceptions:

logical, char,
and all numeric classes can combine with double arrays.

Cell arrays of character vectors can combine with
character arrays or string arrays.

Categorical arrays can combine with character arrays,
cell arrays of character vectors, or string arrays.

Datetime arrays can combine with cell arrays of date
character vectors or single date character vectors.

There are additional requirements for A and B based
on data type:

If A and B are
both ordinal categorical arrays, they must have the same sets of categories,
including their order. If neither A nor B are
ordinal, they need not have the same sets of categories, and the comparison
is performed using the category names. In this case, the categories
of C are the sorted union of the categories from A and B.

If A and B are
tables or timetables, they must have the same variable names (except
for order). For tables, row names are ignored, so that two rows that
have the same values, but different names, are considered equal. For
timetables, row times are taken into account, so that two rows that
have the same values, but different times, are not considered equal.

If A and B are
datetime arrays, they must be consistent with each other in whether
they specify a time zone.

A and B also can be objects
with the following class methods:

sort (or sortrows for
the 'rows' option)

eq

ne

The object class methods must be consistent with each
other. These objects include heterogeneous arrays derived from the
same root class. For example, A and B can
be arrays of handles to graphics objects.

setOrder — Order flag'sorted' (default) | 'stable'

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

Output Arguments

C — Difference of A and Bvector | matrix | table | timetable

Difference of A and B,
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
and A is a row vector, then C is
a row vector.

If the 'rows' flag is not specified
and A is not a row vector, then C is
a column vector.

If the'rows' flag is specified,
then C is a matrix containing the rows of A that
are not in B.

If all the values (or rows) of A are
also in B, then C is an empty
matrix.

The class of C is the same as the class of A,
unless:

A is a character array and B is
a cell array of character vectors, in which case C is
a cell array of character vectors.

A is a character vector, cell array
of character vectors, or string, and B is a categorical
array, in which case C is a categorical array.

A is a cell array of character
vectors or single character vector and B is a datetime
array, in which case C is a datetime array.

A is a character vector or cell
array of character vectors and B is a string array,
in which case C is a string array.

ia — Index to Acolumn 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 are not in B.
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).

Tips

To find the set difference with respect to a subset
of variables from a table or timetable, you can use column subscripting.
For example, you can use setdiff(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

C/C++ Code GenerationGenerate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Code generation does not support cell arrays for the
first or second arguments.

When you do not specify the 'rows' option:

Inputs A and B must
be vectors. If you specify the 'legacy' option,
then inputs A and B must be
row vectors.

The first dimension of a variable-size row vector
must have fixed length 1. The second dimension of a variable-size
column vector must have fixed length 1.

Do not use [] to represent the
empty set. Use a 1-by-0 or 0-by-1 input, for example, zeros(1,0),
to represent the empty set.

If you specify the 'legacy' option,
then empty outputs are row vectors, 1-by-0. They are never
0-by-0.

When you specify both the 'legacy' and 'rows' options,
the output ia is a column vector. If ia is
empty, then it is 0-by-1. It is never
0-by-0, even if the output C is 0-by-0.

When the setOrder is not 'stable' or
when you specify the 'legacy' option, the inputs
must already be sorted in ascending order. The first output, C,
is sorted in ascending order.

Complex inputs must be single or double.

When one input is complex and the other input is real,
do one of the following:

Set setOrder to 'stable'.

Sort the real input in complex ascending order (by
absolute value). Suppose the real input is x. Use sort(complex(x))or sortrows(complex(x)).