Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


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

b

elements to remove from a.

orien
oriented processing:
  • "r": rows of a are searched among b ones.
  • "c": columns of a are searched among b ones.
  • no orien: elements of a are searched among b ones.

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.

v
Array with a's data type and encoding:
  • sorted vector of a's components that are not in b.
  • orien="r": matrix of rows of a that are not in b, sorted in lexicographic order.
  • orien="c": matrix of columns of a that are not in b, sorted in lexicographic order.
ka

vector of linear indices of selected a's components, rows, or columns, such that

  • v = a(ka), or
  • v = a(ka,:) (orien="r"), or
  • v = 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.

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, or strings. Sparse-encoded inputs are also accepted.

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 ↓vkavkavka
row rowrow rowscal rowrow
column colcol colcol colscal
matrix colcol matcol matrow
hypermatrix colcol matcol matrow
scalar scalscal scalscal scalscal

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)
b = grand(2, 4, "uin", 0, 9)
[d, k] = setdiff(a, b);
d, k
--> 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)
b = grand(2, 10, "uin", 0, 3)
[d, k] = setdiff(a, b, "c");
d, k
--> 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

VersionDescription
< 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 >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:08 CEST 2024