<html lang="en"> <head> <title>Set Operations - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <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">26.1 Set Operations</h3> <p>Octave supports the basic set operations. That is, Octave can compute the union, intersection, complement, 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. <!-- ./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-2006"></a></var><br> — Function File: [<var>tf</var>, <var>S_idx</var>] = <b>ismember</b> (<var>A, S</var>)<var><a name="index-ismember-2007"></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-2008"></a></var><br> <blockquote><p>Return a matrix <var>tf</var> with the same shape as <var>A</var> which has a 1 if <code>A(i,j)</code> is in <var>S</var> and 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> <!-- ./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-2009"></a></var><br> — Function File: <b>union</b> (<var>a, b, "rows"</var>)<var><a name="index-union-2010"></a></var><br> <blockquote><p>Return the set of elements that are in either of the sets <var>a</var> and <var>b</var>. 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-2011"></a></var><br> <blockquote> <p>Return index vectors <var>ia</var> and <var>ib</var> such that <code>a == c(ia)</code> and <code>b == c(ib)</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_002dintersect.html#doc_002dintersect">intersect</a>, <a href="doc_002dcomplement.html#doc_002dcomplement">complement</a>, <a href="doc_002dunique.html#doc_002dunique">unique</a>. </p></blockquote></div> <!-- ./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-2012"></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-2013"></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. <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>. <!-- ./set/complement.m --> <p><a name="doc_002dcomplement"></a> <div class="defun"> — Function File: <b>complement</b> (<var>x, y</var>)<var><a name="index-complement-2014"></a></var><br> <blockquote><p>Return the elements of set <var>y</var> that are not in set <var>x</var>. For example, <pre class="example"> complement ([ 1, 2, 3 ], [ 2, 3, 5 ]) ⇒ 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_002dunion.html#doc_002dunion">union</a>, <a href="doc_002dintersect.html#doc_002dintersect">intersect</a>, <a href="doc_002dunique.html#doc_002dunique">unique</a>. </p></blockquote></div> <!-- ./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-2015"></a></var><br> — Function File: <b>setdiff</b> (<var>a, b, "rows"</var>)<var><a name="index-setdiff-2016"></a></var><br> — Function File: [<var>c</var>, <var>i</var>] = <b>setdiff</b> (<var>a, b</var>)<var><a name="index-setdiff-2017"></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. <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> <!-- ./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-2018"></a></var><br> — Function File: <b>setxor</b> (<var>a, b, 'rows'</var>)<var><a name="index-setxor-2019"></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. — Function File: [<var>c</var>, <var>ia</var>, <var>ib</var>] = <b>setxor</b> (<var>a, b</var>)<var><a name="index-setxor-2020"></a></var><br> <blockquote> <p>Return index vectors <var>ia</var> and <var>ib</var> such that <code>a == c(ia)</code> and <code>b == c(ib)</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_002dsetdiff.html#doc_002dsetdiff">setdiff</a>, <a href="doc_002dismember.html#doc_002dismember">ismember</a>. </p></blockquote></div> <!-- DO NOT EDIT! Generated automatically by munge-texi. --> <!-- Copyright (C) 1996, 1997, 1999, 2000, 2002, 2007, 2008, 2009 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>