Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function
setdiff
returns elements or rows or columns of an array that do not belong to another one
Syntax
v = setdiff(a, b) v = setdiff(a, b, orien) [v, ka] = setdiff(..)
Arguments
- a, b
vectors, matrices or hypermatrices of real or complex numbers, encoded integers, booleans, or strings. Sparse-encoded inputs are accepted. Both
a
andb
must have the same data type (and integer type), but may mix dense and sparse encoding.If the option
orien="r"
is used,a
andb
must have the same number of columns. If theorien="c"
is used, they must have the same number of rows.- 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
- Array with
a
's data type and encoding:- 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.
- 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"
)
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.
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.
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.
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.
See also
- unique — extracts (and sorts) distinct elements, rows or columns of a matrix
- union — Set of all elements, rows, or columns of two arrays, without duplicates
- members — count (and locate) in an array each element or row or column of another array
- vectorfind — locates occurences of a (wildcarded) vector in a matrix or hypermatrix
History
Version | Description |
< 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. |
Report an issue | ||
<< intersect | Set operations | union >> |