### 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 =` intersect `(…, "sorted")` ¶
`c =` intersect `(…, "stable")` ¶
`c =` intersect `(…, "legacy")` ¶
`[c, ia, ib] =` intersect `(…)` ¶

  Return the unique elements common to both a and b.

  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 numeric matrices to use this option.

  The optional argument `"sorted"`/`"stable"` controls the order in which unique values appear in the output. The default is `"sorted"` and values in the output are placed in ascending order. The alternative `"stable"` preserves the order found in the input.

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

  Programming Note: The input flag `"legacy"` changes the algorithm to be compatible with MATLAB releases prior to R2012b.

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

`c =` union `(a, b)` ¶
`c =` union `(a, b, "rows")` ¶
`c =` union `(…, "sorted")` ¶
`c =` union `(…, "stable")` ¶
`c =` union `(…, "legacy")` ¶
`[c, ia, ib] =` union `(…)` ¶

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

  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 numeric matrices to use this option.

  The optional argument `"sorted"`/`"stable"` controls the order in which unique values appear in the output. The default is `"sorted"` and values in the output are placed in ascending order. The alternative `"stable"` preserves the order found in the input.

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

  Programming Note: The input flag `"legacy"` changes the algorithm to be compatible with MATLAB releases prior to R2012b.

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

`c =` setdiff `(a, b)` ¶
`c =` setdiff `(a, b, "rows")` ¶
`c =` setdiff `(…, "sorted")` ¶
`c =` setdiff `(…, "stable")` ¶
`c =` setdiff `(…, "legacy")` ¶
`[c, ia] =` setdiff `(…)` ¶

  Return the unique elements in a that are not in b.

  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 numeric matrices to use this option.

  The optional argument `"sorted"`/`"stable"` controls the order in which unique values appear in the output. The default is `"sorted"` and values in the output are placed in ascending order. The alternative `"stable"` preserves the order found in the input.

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

  Programming Note: The input flag `"legacy"` changes the algorithm to be compatible with MATLAB releases prior to R2012b.

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

`c =` setxor `(a, b)` ¶
`c =` setxor `(a, b, "rows")` ¶
`c =` setxor `(…, "sorted")` ¶
`c =` setxor `(…, "stable")` ¶
`c =` setxor `(…, "legacy")` ¶
`[c, ia, ib] =` setxor `(…)` ¶

  Return the unique elements exclusive to sets a or b.

  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 numeric matrices to use this option.

  The optional argument `"sorted"`/`"stable"` controls the order in which unique values appear in the output. The default is `"sorted"` and values in the output are placed in ascending order. The alternative `"stable"` preserves the order found in the input.

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

  Programming Note: The input flag `"legacy"` changes the algorithm to be compatible with MATLAB releases prior to R2012b.

  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, ismembertol.

`tf =` ismembertol `(a, s)` ¶
`tf =` ismembertol `(a, s, tol)` ¶
`tf =` ismembertol `(a, s, name, value)` ¶
`[tf, s_idx] =` ismembertol `(…)` ¶

  Check if values are members of a set within a tolerance.

  This functions returns a logical matrix tf with the same shape as a which is true (1) where the element in a is close to s within a tolerance tol and false (0) if it is not. If tol is not specified, a default tolerance of `1e-6` is used.

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

  The inputs a and s must be numeric values.

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

  Optional property/value pairs may be given to change the function’s behavior. The property may be one of following strings:

  `"ByRows"`

    If set to `false` (default), all elements in a and s are treated separately. If set to `true`, tf will be `true` for each row in a that matches a row in s within the given tolerance. Two rows, u and v, are within tolerance if they fulfill the condition `all (abs (u-v) <= tol*max (abs ([a;s])))`.

  `"OutputAllIndices"`

    If set to `false` (default), s_idx contains indices for one of the matches. If set to `true`, s_idx is a cell array containing the indices for all elements in s that are within tolerance of the corresponding value in a.

  `"DataScale"`

    The provided value DS is used to change the scale factor in the tolerance test to `abs (u-v) <= tol*DS`. By default, the maximum absolute value in a and s is used as the scale factor.

  Example:

      s = [1:6].' * pi;
      a = 10.^log10 (x);
      [tf, s_idx] = ismembertol (a, s);

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

`p =` powerset `(a)` ¶
`p =` 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.