octave: Set Operations
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)’.
DONTPRINTYET See also: unique XREFunique, union XREFunion, *noteDONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
setdiff XREFsetdiff, setxor XREFsetxor, *noteismember:
DONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
setdiff XREFsetdiff, setxor XREFsetxor, ismember
XREFismember.
-- : 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 XREFunique, intersect XREFintersect,
DONTPRINTYET setdiff XREFsetdiff, setxor XREFsetxor, *noteDONTPRINTYET setdiff XREFsetdiff, setxor XREFsetxor,
ismember XREFismember.
-- : 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 column 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)’.
DONTPRINTYET See also: unique XREFunique, union XREFunion, *noteDONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
intersect XREFintersect, setxor XREFsetxor, *noteismember:
DONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
intersect XREFintersect, setxor XREFsetxor, ismember
XREFismember.
-- : 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.
DONTPRINTYET See also: unique XREFunique, union XREFunion, *noteDONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
intersect XREFintersect, setdiff XREFsetdiff, *noteDONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
intersect XREFintersect, setdiff XREFsetdiff,
ismember XREFismember.
-- : 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, 0]
⇒ s_idx = [1, 0]
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];
DONTPRINTYET See also: lookup XREFlookup, unique XREFunique, *noteDONTPRINTYET DONTPRINTYET See also: lookup XREFlookup, unique XREFunique,
union XREFunion, intersect XREFintersect, *notesetdiff:
DONTPRINTYET DONTPRINTYET See also: lookup XREFlookup, unique XREFunique,
union XREFunion, intersect XREFintersect, setdiff
XREFsetdiff, setxor XREFsetxor.
-- : 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.
DONTPRINTYET See also: unique XREFunique, union XREFunion, *noteDONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
intersect XREFintersect, setdiff XREFsetdiff, *notesetxor:
DONTPRINTYET DONTPRINTYET See also: unique XREFunique, union XREFunion,
intersect XREFintersect, setdiff XREFsetdiff, setxor
XREFsetxor, ismember XREFismember.