Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Change language to: Français - Português - 日本語 - Русский
Scilab Help >> Elementary Functions > Set operations > setdiff

# 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, or encoded integers, or strings. Both must have the same data types (and integer types).

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.

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.
v
• 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.

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 → a ↓ v ka v ka v none "r" "c" 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`.

### 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.
```

• unique — extracts (and sorts) distinct elements, rows or columns of a matrix
• union — extract union components of a vector
• 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 Complex numbers are now accepted.