setdiff
returns elements that are in a set but not in another one.
Syntax
v = setdiff(a, b) v = setdiff(a, b, orien) [v, ka] = setdiff(..)
Arguments
- a
a set of elements of type double, boolean, string, sparse, duration, datetime, table and timeseries
- b
elements to remove from
a
.- orien
- oriented processing:
"r"
: rows ofa
are searched amongb
ones."c"
: columns ofa
are searched amongb
ones.- no orien: elements of
a
are searched amongb
ones.
- v
- depending on type of
a
andb
:- sorted vector of
a
's components that are not inb
. orien="r"
: matrix of rows ofa
that are not inb
, sorted in lexicographic order.orien="c"
: matrix of columns ofa
that are not inb
, sorted in lexicographic order.- table or timeseries containing the rows of
a
that are not inb
- sorted vector of
- ka
vector of linear indices of selected
a
's components, rows, or columns, such thatv = a(ka)
, orv = a(ka,:)
(orien="r"
), orv = a(:,ka)
(orien="c"
)
If the option orien="r"
is used, a
and b
must have the same number of columns.
If the orien="c"
is used, they must have the same
number of rows.
This option is not supported for table and timeseries.
Description
setdiff(a, b,..)
computes and returns the elements or rows or columns
of a
that are not in b
.
All duplicates (elements or rows or columns) are removed from a
and
from b
before processing.
Both a
and b
must have the same
data type (and integer type), but may mix dense and sparse encoding.
It can be vectors, matrices or hypermatrices of real or complex numbers, encoded integers, booleans, strings, duration or datetime.
Sparse-encoded inputs are also accepted, as well as table and timeseries.
If a
is an hypermatrix and the "r"
option is used,
a
is replaced with the matrix of all its rows over all its higher
dimensions, before processing.
Same thing if b
is an hypermatrix.
If the "c"
option is used, a
or/and
"b"
are replaced with the respective matrices of all their columns.
If a
and b
are table or timeseries, setdiff(a, b)
returns the sorted unique rows contained
in a
that are not in b
. a
and b
must
have the same variable names. For tables, row names are ignored unlike timeseries where row times are taken into account.
The format of results is presented in the following table, according to the shape of
a
and the orien
option. In all cases, if all
entities of a
are in b
,
[]
is returned for v
as for ka
:
orien → | none | "r" | "c" | |||
---|---|---|---|---|---|---|
a ↓ | v | ka | v | ka | v | ka |
row | row | row | row | scal | row | row |
column | col | col | col | col | col | scal |
matrix | col | col | mat | col | mat | row |
hypermatrix | col | col | mat | col | mat | row |
scalar | scal | scal | scal | scal | scal | scal |
v
and ka
become empty
if a
is empty (whatever is b
), or if all
a
elements are in b
.
For booleans, setdiff(…)
is useful mainly with the "r" or "c" options.
For table and timeseries, the orien
option is ignored (a warning is displayed).
Examples
Example #1:
--> a = grand(1, 10,"uin", 0, 9) a = 2. 2. 4. 5. 4. 1. 9. 5. 8. 3. --> b = grand(2, 4, "uin", 0, 9) b = 5. 0. 9. 9. 5. 6. 0. 4. --> [d, k] = setdiff(a, b); --> d, k d = 1. 2. 3. 8. k = 6. 1. 10. 9.
Example #2: column-wise processing
--> a = grand(2, 7,"uin", 0, 3) a = 0. 1. 0. 2. 3. 0. 2. 2. 2. 2. 1. 0. 1. 2. --> b = grand(2, 10, "uin", 0, 3) b = 1. 1. 3. 1. 1. 1. 3. 0. 2. 0. 3. 3. 2. 2. 0. 0. 1. 0. 1. 0. --> [d, k] = setdiff(a, b, "c"); --> d, k d = 0. 0. 2. 3. 1. 2. 2. 0. k = 6. 1. 7. 5.
Example #3: with some text
v1 = tokens("ab ca ba bb ca cb ba aa cc bc ac aa")' v2 = tokens("cc ac ca bb ac bc ab")' [r, k] = setdiff(v1, v2); r, k
--> v1 = tokens("ab ca ba bb ca cb ba aa cc bc ac aa")' v1 = !ab ca ac bb ca cb ba aa cc bc ac aa ! --> v2 = tokens("cc ac ca bb ac bc ab")' v2 = !cc ac ca bb ac bc ab ! --> [r, k] = setdiff(v1, v2); --> r, k r = !aa ba cb ! k = 8. 3. 6.
setdiff()
for duration and datetime:
A = hours([1 7 7 4 3 6 6]); B = hours([7 5 3 0]); [v, ka] = setdiff(A, B) A = datetime("today") + A; B = datetime("today") + B; [v, ka] = setdiff(A, B)
--> A = hours([1 7 7 4 3 6 6]); --> B = hours([7 5 3 0]); --> [v, ka] = setdiff(A, B) v = [1x3 duration] 01:00:00 04:00:00 06:00:00 ka = [1x3 double] 1. 4. 6. --> A = datetime("today") + A; --> B = datetime("today") + B; --> [v, ka] = setdiff(A, B) v = [1x3 datetime] 2025-08-08 01:00:00 2025-08-08 04:00:00 2025-08-08 06:00:00 ka = [1x3 double] 1. 4. 6.
setdiff()
for table:
A = table([1 7 7 4 3 6 6]', [1:7]') B = table([7 5 3 0]', [2;2;5;5]) [v, kab] = setdiff(A, B) A = table([1;3;4;2], ["d";"c";"f";"h"], [%f;%t;%t; %f], "VariableNames", ["double", "string", "boolean"]) B = table([2;3;4;1], [%t;%t;%t;%t], ["d";"c";"f";"h"], "VariableNames", ["double", "boolean", "string"]) [v, ka] = setdiff(A, B)
--> A = table([1 7 7 4 3 6 6]', [1:7]') A = [7x2 table] Var1 Var2 ____ ____ 1 1 7 2 7 3 4 4 3 5 6 6 6 7 --> B = table([7 5 3 0]', [2;2;5;5]) B = [4x2 table] Var1 Var2 ____ ____ 7 2 5 2 3 5 0 5 --> [v, kab] = setdiff(A, B) v = [5x2 table] Var1 Var2 ____ ____ 1 1 4 4 6 6 6 7 7 3 kab = [5x1 double] 1. 4. 6. 7. 3. --> A = table([1;3;4;2], ["d";"c";"f";"h"], [%f;%t;%t; %f], "VariableNames", ["double", "string", "boolean"]) A = [4x3 table] double string boolean ______ ______ _______ 1 d F 3 c T 4 f T 2 h F --> B = table([2;3;4;1], [%t;%t;%t;%t], ["d";"c";"f";"h"], "VariableNames", ["double", "boolean", "string"]) B = [4x3 table] double boolean string ______ _______ ______ 2 T d 3 T c 4 T f 1 T h --> [v, ka] = setdiff(A, B) v = [2x3 table] double string boolean ______ ______ _______ 1 d F 2 h F ka = [2x1 double] 1. 4.
setdiff()
for timeseries:
A = timeseries(hours([1:10])', [2 7 0 3 6 6 8 6 8 0]') B = timeseries(hours(1:2:15)', [2 9 6 8 8 3 0 0]') [v, ka] = setdiff(A, B)
--> A = timeseries(hours([1:10])', [2 7 0 3 6 6 8 6 8 0]') A = [10x1 timeseries] Time Var1 ________ ____ 01:00:00 2 02:00:00 7 03:00:00 0 04:00:00 3 05:00:00 6 06:00:00 6 07:00:00 8 08:00:00 6 09:00:00 8 10:00:00 0 --> B = timeseries(hours(1:2:15)', [2 9 6 8 8 3 0 0]') B = [8x1 timeseries] Time Var1 ________ ____ 01:00:00 2 03:00:00 9 05:00:00 6 07:00:00 8 09:00:00 8 11:00:00 3 13:00:00 0 15:00:00 0 --> [v, ka] = setdiff(A, B) v = [6x1 timeseries] Time Var1 ________ ____ 02:00:00 7 03:00:00 0 04:00:00 3 06:00:00 6 08:00:00 6 10:00:00 0 ka = [6x1 double] 2. 3. 4. 6. 8. 10.
See also
- unique — ベクトルまたは行列のユニークなな要素を展開
- union — Set of all elements, rows, or columns of two arrays, without duplicates
- members — 配列の各要素または他の配列の行または列を数える(及び位置を調べる)
- vectorfind — locates occurences of a (wildcarded) vector in a matrix or hypermatrix
History
バージョン | 記述 |
< 5.0 | Function introduced. |
6.0.2 | Option "r" | "c" added, including for hypermatrices. |
6.1.0 | Extension to complex numbers. |
6.1.1 | Boolean inputs and sparse inputs (boolean or numeric) are now accepted. |
2026.0.0 | Support for duration, datetime, table and timeseries types added. |
Report an issue | ||
<< intersect | setoperations | union >> |