Set Operations - Untitled
Description: Untitled

Up: Sets

### 26.1 Set Operations

Octave supports the basic set operations. That is, Octave can compute the union, intersection, complement, and difference of two sets. Octave can also supports the Exclusive Or set operation, and membership determination. The functions for set operations all work in pretty much the same way. As an example, assume that `x` and `y` contains two sets, then

         union(x, y)

computes the union of the two sets.

— Function File: [tf, a_idx] = ismember (A, S)
— Function File: [tf, a_idx] = ismember (A, S, "rows")

> Return a matrix tf the same shape as A which has 1 if `A(i,j)` is in S or 0 if it isn't. If a second output argument is requested, the indexes into S of the matching elements are also returned.

>               a = [3, 10, 1];
>               s = [0:9];
>               [tf, a_idx] = residue (a, s);
>                tf = [1, 0, 1]
>                a_idx = [4, 0, 2]

> The inputs, A and S, may also be cell arrays.

>               a = {'abc'};
>               s = {'abc', 'def'};
>               [tf, a_idx] = residue (a, s);
>                tf = [1, 0]
>                a_idx = [1, 0]

> With the optional third argument `"rows"`, and matrices A and S with the same number of columns, compare rows in A with the rows in S.

>               a = [1:3; 5:7; 4:6];
>               s = [0:2; 1:3; 2:4; 3:5; 4:6];
>               [tf, a_idx] = ismember(a, s, 'rows');
>                tf = logical ([1; 0; 1])
>                a_idx = [2; 0; 5];

> See also: unique, union, intersection, setxor, setdiff.

— Function File: union (x, y)

> Return the set of elements that are in either of the sets x and y. For example,

>               union ([ 1, 2, 4 ], [ 2, 3, 5 ])
>                [ 1, 2, 3, 4, 5 ]

> See also: create_set, intersection, complement.

— Function File: intersect (a, b)
— Function File: [c, ia, ib] = intersect (a, b)

> Return the elements in both a and b, sorted in ascending order. If a and b are both column vectors return a column vector, otherwise return a row vector.

> Return index vectors ia and ib such that `a(ia)==c` and `b(ib)==c`.

See also: unique, union, setxor, setdiff, ismember.

— Function File: complement (x, y)

> Return the elements of set y that are not in set x. For example,

>               complement ([ 1, 2, 3 ], [ 2, 3, 5 ])
>                5

> See also: create_set, union, intersection.

— Function File: setdiff (a, b)
— Function File: setdiff (a, b, "rows")

> Return the elements in a that are not in b, sorted in ascending order. If a and b are both column vectors return a column vector, otherwise return a row vector.

> Given the optional third argument ‘`"rows"`’, return the rows in a that are not in b, sorted in ascending order by rows.

> See also: unique, union, intersect, setxor, ismember.

— Function File: setxor (a, b)

> Return the elements exclusive to a or b, sorted in ascending order. If a and b are both column vectors return a column vector, otherwise return a row vector.

> See also: unique, union, intersect, setdiff, ismember.