Up: Sets


27.1 Set Operations

Octave supports the basic set operations. That is, Octave can compute the union, intersection, and difference of two sets. Octave 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 = ismember (A, s)
— Function File: [tf, S_idx] = ismember (A, s)
— Function File: [tf, S_idx] = ismember (A, s, "rows")

Return a logical matrix tf with the same shape as A which is true (1) if A(i,j) is in s and false (0) if it is not. If a second output argument is requested, the index into s of each of the matching elements 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, 0]
               ⇒ s_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, s_idx] = ismember(a, s, 'rows');
               ⇒ tf = logical ([1; 0; 1])
               ⇒ s_idx = [2; 0; 5];

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

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

Return the set of elements that are in either of the sets a and b. a, b may be cell arrays of string(s). For example:

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

If the optional third input argument is the string "rows" each row of the matrices a and b will be considered an element of sets. For example:

          union([1, 2; 2, 3], [1, 2; 3, 4], "rows")
               ⇒  1   2
              2   3
              3   4
— Function File: [c, ia, ib] = union (a, b)

Return index vectors ia and ib such that a(ia) and b(ib) are disjoint sets whose union is c.

See also: intersect, setdiff, unique.

— 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. a, b may be cell arrays of string(s).

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

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

— Function File: setdiff (a, b)
— Function File: setdiff (a, b, "rows")
— Function File: [c, i] = setdiff (a, b)

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. a, b may be cell arrays of string(s).

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

If requested, return i such that c = a(i).

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

— Function File: setxor (a, b)
— Function File: setxor (a, b, 'rows')
— Function File: [c, ia, ib] = 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. a, b may be cell arrays of string(s).

With three output arguments, 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.

— Function File: powerset (a)
— Function File: powerset (a, "rows")

Return a cell array containing all subsets of the set a.

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