Set Operations (GNU Octave (version 5.1.0))

Up: Sets [Contents][Index]

### 27.1 Set Operations

Octave supports several basic set operations. Octave can compute the union, intersection, and difference of two sets. Octave also supports the Exclusive Or set operation.

The functions for set operations all work in the same way by accepting two input sets and returning a third set. As an example, assume that `a` and `b` contains two sets, then

    union (a, b)

computes the union of the two sets.

Finally, determining whether elements belong to a set can be done with the `ismember` function. Because sets are ordered this operation is very efficient and is of order O(log2(n)) which is preferable to the `find` function which is of order O(n).

c = intersect (a, b)
c = intersect (a, b, "rows")
[c, ia, ib] = intersect (…)

  Return the unique elements common to both a and b sorted in ascending order.

  If a and b are both row vectors then return a row vector; Otherwise, return a column vector. The inputs may also be cell arrays of strings.

  If the optional input `"rows"` is given then return the common rows of a and b. The inputs must be 2-D matrices to use this option.

  If requested, return index vectors ia and ib such that `c = a(ia)` and `c = b(ib)`.

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

c = union (a, b)
c = union (a, b, "rows")
[c, ia, ib] = union (…)

  Return the unique elements that are in either a or b sorted in ascending order.

  If a and b are both row vectors then return a row vector; Otherwise, return a column vector. The inputs may also be cell arrays of strings.

  If the optional input `"rows"` is given then return rows that are in either a or b. The inputs must be 2-D matrices to use this option.

  The optional outputs ia and ib are index vectors such that `a(ia)` and `b(ib)` are disjoint sets whose union is c.

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

c = setdiff (a, b)
c = setdiff (a, b, "rows")
[c, ia] = setdiff (…)

  Return the unique elements in a that are not in b sorted in ascending order.

  If a is a row vector return a row vector; Otherwise, return a column vector. The inputs may also be cell arrays of strings.

  If the optional input `"rows"` is given then return the rows in a that are not in b. The inputs must be 2-D matrices to use this option.

  If requested, return the index vector ia such that `c = a(ia)`.

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

c = setxor (a, b)
c = setxor (a, b, "rows")
[c, ia, ib] = setxor (…)

  Return the unique elements exclusive to sets a or b sorted in ascending order.

  If a and b are both row vectors then return a row vector; Otherwise, return a column vector. The inputs may also be cell arrays of strings.

  If the optional input `"rows"` is given then return the rows exclusive to sets a and b. The inputs must be 2-D matrices to use this option.

  If requested, return index vectors ia and ib such that `a(ia)` and `b(ib)` are disjoint sets whose union is c.

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

tf = ismember (a, s)
tf = ismember (a, s, "rows")
[tf, s_idx] = ismember (…)

  Return a logical matrix tf with the same shape as a which is true (1) if the element in a is found in s and false (0) if it is not.

  If a second output argument is requested then the index into s of each matching element is also returned.

      a = [3, 10, 1];
      s = [0:9];
      [tf, s_idx] = ismember (a, s)
           ⇒ tf = [1, 0, 1]
           ⇒ s_idx = [4, 0, 2]

  The inputs a and s may also be cell arrays.

      a = {"abc"};
      s = {"abc", "def"};
      [tf, s_idx] = ismember (a, s)
           ⇒ tf = 1
           ⇒ s_idx = 1

  If the optional third argument `"rows"` is given then compare rows in a with rows in s. The inputs must be 2-D matrices with the same number of columns to use this option.

      a = [1:3; 5:7; 4:6];
      s = [0:2; 1:3; 2:4; 3:5; 4:6];
      [tf, s_idx] = ismember (a, s, "rows")
           ⇒ tf = logical ([1; 0; 1])
           ⇒ s_idx = [2; 0; 5];

  See also: lookup, unique, union, intersect, setdiff, setxor.

powerset (a)
powerset (a, "rows")

  Compute the powerset (all subsets) of the set a.

  The set a must be a numerical matrix or a cell array of strings. The output will always be a cell array of either vectors or strings.

  With the optional argument `"rows"`, each row of the set a is considered one element of the set. The input must be a 2-D numeric matrix to use this argument.

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

Up: Sets [Contents][Index]