<html lang="en"> <head> <title>Set Operations - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Sets.html#Sets" title="Sets"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Set-Operations"></a> <p> Up: <a rel="up" accesskey="u" href="Sets.html#Sets">Sets</a> <hr> </div> <h3 class="section">27.1 Set Operations</h3> <p>Octave supports the basic set operations. That is, Octave can compute the union, intersection, and difference of two sets. Octave also supports the <em>Exclusive Or</em> set operation, and membership determination. The functions for set operations all work in pretty much the same way. As an example, assume that <code>x</code> and <code>y</code> contains two sets, then <pre class="example"> union(x, y) </pre> <p class="noindent">computes the union of the two sets. <!-- ismember scripts/set/ismember.m --> <p><a name="doc_002dismember"></a> <div class="defun"> — Function File: <var>tf</var> = <b>ismember</b> (<var>A, s</var>)<var><a name="index-ismember-2684"></a></var><br> — Function File: [<var>tf</var>, <var>S_idx</var>] = <b>ismember</b> (<var>A, s</var>)<var><a name="index-ismember-2685"></a></var><br> — Function File: [<var>tf</var>, <var>S_idx</var>] = <b>ismember</b> (<var>A, s, "rows"</var>)<var><a name="index-ismember-2686"></a></var><br> <blockquote><p>Return a logical matrix <var>tf</var> with the same shape as <var>A</var> which is true (1) if <code>A(i,j)</code> is in <var>s</var> and false (0) if it is not. If a second output argument is requested, the index into <var>s</var> of each of the matching elements is also returned. <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> <p>The inputs, <var>A</var> and <var>s</var>, may also be cell arrays. <pre class="example"> a = {'abc'}; s = {'abc', 'def'}; [tf, s_idx] = ismember (a, s) ⇒ tf = [1, 0] ⇒ s_idx = [1, 0] </pre> <p>With the optional third argument <code>"rows"</code>, and matrices <var>A</var> and <var>s</var> with the same number of columns, compare rows in <var>A</var> with the rows in <var>s</var>. <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> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dunique.html#doc_002dunique">unique</a>, <a href="doc_002dunion.html#doc_002dunion">union</a>, <a href="doc_002dintersect.html#doc_002dintersect">intersect</a>, <a href="doc_002dsetxor.html#doc_002dsetxor">setxor</a>, <a href="doc_002dsetdiff.html#doc_002dsetdiff">setdiff</a>. </p></blockquote></div> <!-- union scripts/set/union.m --> <p><a name="doc_002dunion"></a> <div class="defun"> — Function File: <b>union</b> (<var>a, b</var>)<var><a name="index-union-2687"></a></var><br> — Function File: <b>union</b> (<var>a, b, "rows"</var>)<var><a name="index-union-2688"></a></var><br> <blockquote><p>Return the set of elements that are in either of the sets <var>a</var> and <var>b</var>. <var>a</var>, <var>b</var> may be cell arrays of string(s). For example: <pre class="example"> union ([1, 2, 4], [2, 3, 5]) ⇒ [1, 2, 3, 4, 5] </pre> <p>If the optional third input argument is the string "rows" each row of the matrices <var>a</var> and <var>b</var> will be considered an element of sets. For example: <pre class="example"> union ([1, 2; 2, 3], [1, 2; 3, 4], "rows") ⇒ 1 2 2 3 3 4 </pre> — Function File: [<var>c</var>, <var>ia</var>, <var>ib</var>] = <b>union</b> (<var>a, b</var>)<var><a name="index-union-2689"></a></var><br> <blockquote> <p>Return index vectors <var>ia</var> and <var>ib</var> such that <code>a(ia)</code> and <code>b(ib)</code> are disjoint sets whose union is <var>c</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dintersect.html#doc_002dintersect">intersect</a>, <a href="doc_002dsetdiff.html#doc_002dsetdiff">setdiff</a>, <a href="doc_002dunique.html#doc_002dunique">unique</a>. </p></blockquote></div> <!-- intersect scripts/set/intersect.m --> <p><a name="doc_002dintersect"></a> <div class="defun"> — Function File: <b>intersect</b> (<var>a, b</var>)<var><a name="index-intersect-2690"></a></var><br> — Function File: [<var>c</var>, <var>ia</var>, <var>ib</var>] = <b>intersect</b> (<var>a, b</var>)<var><a name="index-intersect-2691"></a></var><br> <blockquote> <p>Return the elements in both <var>a</var> and <var>b</var>, sorted in ascending order. If <var>a</var> and <var>b</var> are both column vectors return a column vector, otherwise return a row vector. <var>a</var>, <var>b</var> may be cell arrays of string(s). <p>Return index vectors <var>ia</var> and <var>ib</var> such that <code>a(ia)==c</code> and <code>b(ib)==c</code>. </blockquote></div> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dunique.html#doc_002dunique">unique</a>, <a href="doc_002dunion.html#doc_002dunion">union</a>, <a href="doc_002dsetxor.html#doc_002dsetxor">setxor</a>, <a href="doc_002dsetdiff.html#doc_002dsetdiff">setdiff</a>, <a href="doc_002dismember.html#doc_002dismember">ismember</a>. <!-- setdiff scripts/set/setdiff.m --> <p><a name="doc_002dsetdiff"></a> <div class="defun"> — Function File: <b>setdiff</b> (<var>a, b</var>)<var><a name="index-setdiff-2692"></a></var><br> — Function File: <b>setdiff</b> (<var>a, b, "rows"</var>)<var><a name="index-setdiff-2693"></a></var><br> — Function File: [<var>c</var>, <var>i</var>] = <b>setdiff</b> (<var>a, b</var>)<var><a name="index-setdiff-2694"></a></var><br> <blockquote><p>Return the elements in <var>a</var> that are not in <var>b</var>, sorted in ascending order. If <var>a</var> and <var>b</var> are both column vectors return a column vector, otherwise return a row vector. <var>a</var>, <var>b</var> may be cell arrays of string(s). <p>Given the optional third argument ‘<samp><span class="samp">"rows"</span></samp>’, return the rows in <var>a</var> that are not in <var>b</var>, sorted in ascending order by rows. <p>If requested, return <var>i</var> such that <code>c = a(i)</code>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dunique.html#doc_002dunique">unique</a>, <a href="doc_002dunion.html#doc_002dunion">union</a>, <a href="doc_002dintersect.html#doc_002dintersect">intersect</a>, <a href="doc_002dsetxor.html#doc_002dsetxor">setxor</a>, <a href="doc_002dismember.html#doc_002dismember">ismember</a>. </p></blockquote></div> <!-- setxor scripts/set/setxor.m --> <p><a name="doc_002dsetxor"></a> <div class="defun"> — Function File: <b>setxor</b> (<var>a, b</var>)<var><a name="index-setxor-2695"></a></var><br> — Function File: <b>setxor</b> (<var>a, b, 'rows'</var>)<var><a name="index-setxor-2696"></a></var><br> — Function File: [<var>c</var>, <var>ia</var>, <var>ib</var>] = <b>setxor</b> (<var>a, b</var>)<var><a name="index-setxor-2697"></a></var><br> <blockquote> <p>Return the elements exclusive to <var>a</var> or <var>b</var>, sorted in ascending order. If <var>a</var> and <var>b</var> are both column vectors return a column vector, otherwise return a row vector. <var>a</var>, <var>b</var> may be cell arrays of string(s). <p>With three output arguments, return index vectors <var>ia</var> and <var>ib</var> such that <code>a(ia)</code> and <code>b(ib)</code> are disjoint sets whose union is <var>c</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dunique.html#doc_002dunique">unique</a>, <a href="doc_002dunion.html#doc_002dunion">union</a>, <a href="doc_002dintersect.html#doc_002dintersect">intersect</a>, <a href="doc_002dsetdiff.html#doc_002dsetdiff">setdiff</a>, <a href="doc_002dismember.html#doc_002dismember">ismember</a>. </p></blockquote></div> <!-- powerset scripts/set/powerset.m --> <p><a name="doc_002dpowerset"></a> <div class="defun"> — Function File: <b>powerset</b> (<var>a</var>)<var><a name="index-powerset-2698"></a></var><br> — Function File: <b>powerset</b> (<var>a, "rows"</var>)<var><a name="index-powerset-2699"></a></var><br> <blockquote> <p>Return a cell array containing all subsets of the set <var>a</var>. </blockquote></div> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dunique.html#doc_002dunique">unique</a>, <a href="doc_002dunion.html#doc_002dunion">union</a>, <a href="doc_002dsetxor.html#doc_002dsetxor">setxor</a>, <a href="doc_002dsetdiff.html#doc_002dsetdiff">setdiff</a>, <a href="doc_002dismember.html#doc_002dismember">ismember</a>. <!-- DO NOT EDIT! Generated automatically by munge-texi.pl. --> <!-- Copyright (C) 1996-2012 John W. Eaton --> <!-- This file is part of Octave. --> <!-- Octave is free software; you can redistribute it and/or modify it --> <!-- under the terms of the GNU General Public License as published by the --> <!-- Free Software Foundation; either version 3 of the License, or (at --> <!-- your option) any later version. --> <!-- Octave is distributed in the hope that it will be useful, but WITHOUT --> <!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or --> <!-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --> <!-- for more details. --> <!-- You should have received a copy of the GNU General Public License --> <!-- along with Octave; see the file COPYING. If not, see --> <!-- <http://www.gnu.org/licenses/>. --> </body></html>