1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Set Operations (GNU Octave (version 7.3.0))</title>
<meta name="description" content="Set Operations (GNU Octave (version 7.3.0))">
<meta name="keywords" content="Set Operations (GNU Octave (version 7.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Sets.html" rel="up" title="Sets">
<style type="text/css">
<!--
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="section" id="Set-Operations">
<div class="header">
<p>
Up: <a href="Sets.html" accesskey="u" rel="up">Sets</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Set-Operations-1"></span><h3 class="section">27.1 Set Operations</h3>
<p>Octave supports several basic set operations. Octave can compute the union,
intersection, and difference of two sets. Octave also supports the
<em>Exclusive Or</em> set operation.
</p>
<p>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 <code>a</code> and
<code>b</code> contains two sets, then
</p>
<div class="example">
<pre class="example">union (a, b)
</pre></div>
<p>computes the union of the two sets.
</p>
<p>Finally, determining whether elements belong to a set can be done with the
<code>ismember</code> function. Because sets are ordered this operation is very
efficient and is of order O(log2(n)) which is preferable to the <code>find</code>
function which is of order O(n).
</p>
<span id="XREFintersect"></span><dl class="def">
<dt id="index-intersect"><span class="category">: </span><span><em><var>c</var> =</em> <strong>intersect</strong> <em>(<var>a</var>, <var>b</var>)</em><a href='#index-intersect' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-intersect-1"><span class="category">: </span><span><em><var>c</var> =</em> <strong>intersect</strong> <em>(<var>a</var>, <var>b</var>, "rows")</em><a href='#index-intersect-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-intersect-2"><span class="category">: </span><span><em><var>c</var> =</em> <strong>intersect</strong> <em>(…, "sorted")</em><a href='#index-intersect-2' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-intersect-3"><span class="category">: </span><span><em><var>c</var> =</em> <strong>intersect</strong> <em>(…, "stable")</em><a href='#index-intersect-3' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-intersect-4"><span class="category">: </span><span><em><var>c</var> =</em> <strong>intersect</strong> <em>(…, "legacy")</em><a href='#index-intersect-4' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-intersect-5"><span class="category">: </span><span><em>[<var>c</var>, <var>ia</var>, <var>ib</var>] =</em> <strong>intersect</strong> <em>(…)</em><a href='#index-intersect-5' class='copiable-anchor'> ¶</a></span></dt>
<dd>
<p>Return the unique elements common to both <var>a</var> and <var>b</var>.
</p>
<p>If <var>a</var> and <var>b</var> are both row vectors then return a row vector;
Otherwise, return a column vector. The inputs may also be cell arrays of
strings.
</p>
<p>If the optional input <code>"rows"</code> is given then return the common rows of
<var>a</var> and <var>b</var>. The inputs must be 2-D numeric matrices to use this
option.
</p>
<p>The optional argument <code>"sorted"</code>/<code>"stable"</code> controls the order
in which unique values appear in the output. The default is
<code>"sorted"</code> and values in the output are placed in ascending order.
The alternative <code>"stable"</code> preserves the order found in the input.
</p>
<p>If requested, return column index vectors <var>ia</var> and <var>ib</var> such that
<code><var>c</var> = <var>a</var>(<var>ia</var>)</code> and <code><var>c</var> = <var>b</var>(<var>ib</var>)</code>.
</p>
<p>Programming Note: The input flag <code>"legacy"</code> changes the algorithm
to be compatible with <small>MATLAB</small> releases prior to R2012b.
</p>
<p><strong>See also:</strong> <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFunion">union</a>, <a href="#XREFsetdiff">setdiff</a>, <a href="#XREFsetxor">setxor</a>, <a href="#XREFismember">ismember</a>.
</p></dd></dl>
<span id="XREFunion"></span><dl class="def">
<dt id="index-union"><span class="category">: </span><span><em><var>c</var> =</em> <strong>union</strong> <em>(<var>a</var>, <var>b</var>)</em><a href='#index-union' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-union-1"><span class="category">: </span><span><em><var>c</var> =</em> <strong>union</strong> <em>(<var>a</var>, <var>b</var>, "rows")</em><a href='#index-union-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-union-2"><span class="category">: </span><span><em><var>c</var> =</em> <strong>union</strong> <em>(…, "sorted")</em><a href='#index-union-2' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-union-3"><span class="category">: </span><span><em><var>c</var> =</em> <strong>union</strong> <em>(…, "stable")</em><a href='#index-union-3' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-union-4"><span class="category">: </span><span><em><var>c</var> =</em> <strong>union</strong> <em>(…, "legacy")</em><a href='#index-union-4' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-union-5"><span class="category">: </span><span><em>[<var>c</var>, <var>ia</var>, <var>ib</var>] =</em> <strong>union</strong> <em>(…)</em><a href='#index-union-5' class='copiable-anchor'> ¶</a></span></dt>
<dd>
<p>Return the unique elements that are in either <var>a</var> or <var>b</var>.
</p>
<p>If <var>a</var> and <var>b</var> are both row vectors then return a row vector;
Otherwise, return a column vector. The inputs may also be cell arrays of
strings.
</p>
<p>If the optional input <code>"rows"</code> is given then return rows that are in
either <var>a</var> or <var>b</var>. The inputs must be 2-D numeric matrices to use
this option.
</p>
<p>The optional argument <code>"sorted"</code>/<code>"stable"</code> controls the order
in which unique values appear in the output. The default is
<code>"sorted"</code> and values in the output are placed in ascending order.
The alternative <code>"stable"</code> preserves the order found in the input.
</p>
<p>The optional outputs <var>ia</var> and <var>ib</var> are column index vectors such
that <code><var>a</var>(<var>ia</var>)</code> and <code><var>b</var>(<var>ib</var>)</code> are disjoint sets
whose union is <var>c</var>.
</p>
<p>Programming Note: The input flag <code>"legacy"</code> changes the algorithm
to be compatible with <small>MATLAB</small> releases prior to R2012b.
</p>
<p><strong>See also:</strong> <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFintersect">intersect</a>, <a href="#XREFsetdiff">setdiff</a>, <a href="#XREFsetxor">setxor</a>, <a href="#XREFismember">ismember</a>.
</p></dd></dl>
<span id="XREFsetdiff"></span><dl class="def">
<dt id="index-setdiff"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setdiff</strong> <em>(<var>a</var>, <var>b</var>)</em><a href='#index-setdiff' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setdiff-1"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setdiff</strong> <em>(<var>a</var>, <var>b</var>, "rows")</em><a href='#index-setdiff-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setdiff-2"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setdiff</strong> <em>(…, "sorted")</em><a href='#index-setdiff-2' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setdiff-3"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setdiff</strong> <em>(…, "stable")</em><a href='#index-setdiff-3' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setdiff-4"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setdiff</strong> <em>(…, "legacy")</em><a href='#index-setdiff-4' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setdiff-5"><span class="category">: </span><span><em>[<var>c</var>, <var>ia</var>] =</em> <strong>setdiff</strong> <em>(…)</em><a href='#index-setdiff-5' class='copiable-anchor'> ¶</a></span></dt>
<dd><p>Return the unique elements in <var>a</var> that are not in <var>b</var>.
</p>
<p>If <var>a</var> is a row vector return a row vector; Otherwise, return a
column vector. The inputs may also be cell arrays of strings.
</p>
<p>If the optional input <code>"rows"</code> is given then return the rows in
<var>a</var> that are not in <var>b</var>. The inputs must be 2-D numeric matrices to
use this option.
</p>
<p>The optional argument <code>"sorted"</code>/<code>"stable"</code> controls the order
in which unique values appear in the output. The default is
<code>"sorted"</code> and values in the output are placed in ascending order.
The alternative <code>"stable"</code> preserves the order found in the input.
</p>
<p>If requested, return the index vector <var>ia</var> such that
<code><var>c</var> = <var>a</var>(<var>ia</var>)</code>.
</p>
<p>Programming Note: The input flag <code>"legacy"</code> changes the algorithm
to be compatible with <small>MATLAB</small> releases prior to R2012b.
</p>
<p><strong>See also:</strong> <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFunion">union</a>, <a href="#XREFintersect">intersect</a>, <a href="#XREFsetxor">setxor</a>, <a href="#XREFismember">ismember</a>.
</p></dd></dl>
<span id="XREFsetxor"></span><dl class="def">
<dt id="index-setxor"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setxor</strong> <em>(<var>a</var>, <var>b</var>)</em><a href='#index-setxor' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setxor-1"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setxor</strong> <em>(<var>a</var>, <var>b</var>, "rows")</em><a href='#index-setxor-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setxor-2"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setxor</strong> <em>(…, "sorted")</em><a href='#index-setxor-2' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setxor-3"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setxor</strong> <em>(…, "stable")</em><a href='#index-setxor-3' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setxor-4"><span class="category">: </span><span><em><var>c</var> =</em> <strong>setxor</strong> <em>(…, "legacy")</em><a href='#index-setxor-4' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-setxor-5"><span class="category">: </span><span><em>[<var>c</var>, <var>ia</var>, <var>ib</var>] =</em> <strong>setxor</strong> <em>(…)</em><a href='#index-setxor-5' class='copiable-anchor'> ¶</a></span></dt>
<dd>
<p>Return the unique elements exclusive to sets <var>a</var> or <var>b</var>.
</p>
<p>If <var>a</var> and <var>b</var> are both row vectors then return a row vector;
Otherwise, return a column vector. The inputs may also be cell arrays of
strings.
</p>
<p>If the optional input <code>"rows"</code> is given then return the rows exclusive
to sets <var>a</var> and <var>b</var>. The inputs must be 2-D numeric matrices to use
this option.
</p>
<p>The optional argument <code>"sorted"</code>/<code>"stable"</code> controls the order
in which unique values appear in the output. The default is
<code>"sorted"</code> and values in the output are placed in ascending order.
The alternative <code>"stable"</code> preserves the order found in the input.
</p>
<p>The optional outputs <var>ia</var> and <var>ib</var> are column index vectors such
that <code><var>a</var>(<var>ia</var>)</code> and <code><var>b</var>(<var>ib</var>)</code> are disjoint sets
whose union is <var>c</var>.
</p>
<p>Programming Note: The input flag <code>"legacy"</code> changes the algorithm
to be compatible with <small>MATLAB</small> releases prior to R2012b.
</p>
<p><strong>See also:</strong> <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFunion">union</a>, <a href="#XREFintersect">intersect</a>, <a href="#XREFsetdiff">setdiff</a>, <a href="#XREFismember">ismember</a>.
</p></dd></dl>
<span id="XREFismember"></span><dl class="def">
<dt id="index-ismember"><span class="category">: </span><span><em><var>tf</var> =</em> <strong>ismember</strong> <em>(<var>a</var>, <var>s</var>)</em><a href='#index-ismember' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-ismember-1"><span class="category">: </span><span><em><var>tf</var> =</em> <strong>ismember</strong> <em>(<var>a</var>, <var>s</var>, "rows")</em><a href='#index-ismember-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-ismember-2"><span class="category">: </span><span><em>[<var>tf</var>, <var>s_idx</var>] =</em> <strong>ismember</strong> <em>(…)</em><a href='#index-ismember-2' class='copiable-anchor'> ¶</a></span></dt>
<dd>
<p>Return a logical matrix <var>tf</var> with the same shape as <var>a</var> which is
true (1) if the element in <var>a</var> is found in <var>s</var> and false (0) if it
is not.
</p>
<p>If a second output argument is requested then the index into <var>s</var> of each
matching element is also returned.
</p>
<div class="example">
<pre class="example">a = [3, 10, 1];
s = [0:9];
[tf, s_idx] = ismember (a, s)
⇒ tf = [1, 0, 1]
⇒ s_idx = [4, 0, 2]
</pre></div>
<p>The inputs <var>a</var> and <var>s</var> may also be cell arrays.
</p>
<div class="example">
<pre class="example">a = {"abc"};
s = {"abc", "def"};
[tf, s_idx] = ismember (a, s)
⇒ tf = 1
⇒ s_idx = 1
</pre></div>
<p>If the optional third argument <code>"rows"</code> is given then compare rows
in <var>a</var> with rows in <var>s</var>. The inputs must be 2-D matrices with the
same number of columns to use this option.
</p>
<div class="example">
<pre class="example">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];
</pre></div>
<p><strong>See also:</strong> <a href="Finding-Elements-and-Checking-Conditions.html#XREFlookup">lookup</a>, <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFunion">union</a>, <a href="#XREFintersect">intersect</a>, <a href="#XREFsetdiff">setdiff</a>, <a href="#XREFsetxor">setxor</a>.
</p></dd></dl>
<span id="XREFpowerset"></span><dl class="def">
<dt id="index-powerset"><span class="category">: </span><span><em></em> <strong>powerset</strong> <em>(<var>a</var>)</em><a href='#index-powerset' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-powerset-1"><span class="category">: </span><span><em></em> <strong>powerset</strong> <em>(<var>a</var>, "rows")</em><a href='#index-powerset-1' class='copiable-anchor'> ¶</a></span></dt>
<dd><p>Compute the powerset (all subsets) of the set <var>a</var>.
</p>
<p>The set <var>a</var> must be a numerical matrix or a cell array of strings. The
output will always be a cell array of either vectors or strings.
</p>
<p>With the optional argument <code>"rows"</code>, each row of the set <var>a</var> is
considered one element of the set. The input must be a 2-D numeric matrix
to use this argument.
</p>
<p><strong>See also:</strong> <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFunion">union</a>, <a href="#XREFintersect">intersect</a>, <a href="#XREFsetdiff">setdiff</a>, <a href="#XREFsetxor">setxor</a>, <a href="#XREFismember">ismember</a>.
</p></dd></dl>
</div>
<hr>
<div class="header">
<p>
Up: <a href="Sets.html">Sets</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|