1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2008 - INRIA
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 * Copyright (C) 2018 - 2020 - Samuel GOUGEON
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
17 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
18 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
19 xmlns:scilab="http://www.scilab.org" xml:id="intersect" xml:lang="en">
21 <refname>intersect</refname>
23 elements or rows or columns met in both input arrays, without duplicates
30 M = intersect(a, b, orient)
31 [M, ka] = intersect(..)
32 [M, ka, kb] = intersect(..)
36 <title>Arguments</title>
41 vectors, matrices or hypermatrices of booleans, encoded integers, real or
42 complex numbers, or text. <varname>a</varname> and <varname>b</varname> must
43 have the same datatype.
44 For text inputs, UTF characters are accepted.
45 Sparse numeric or boolean matrices are accepted : Either <varname>a</varname> or
46 <varname>b</varname> or both <varname>a</varname> and <varname>b</varname> may
54 flag with possible values : 1 or "r", 2 or "c". Can't be used if
55 <varname>a</varname> or/and <varname>b</varname> is an hypermatrix.
63 matrix of the same datatype as <varname>a</varname> and <varname>b</varname>.
66 Without <varname>orient</varname>: <varname>M</varname> is a row vector.
69 With <literal>orient="r"|1</literal>: <varname>M</varname> is a
70 matrix stacking the common rows of <varname>a</varname> and
74 With <literal>orient="c"|2</literal>: <varname>M</varname> is a
75 matrix stacking the common columns of <varname>a</varname> and
81 <varname>M</varname> is sparse as soon as either <varname>a</varname> or
82 <varname>b</varname> is sparse and none is empty.
89 Dense row vector of indices in <varname>a</varname>.
96 Dense row vector of indices in <varname>b</varname>.
103 <title>Description</title>
105 <literal>intersect(a,b)</literal> returns a row vector of unique values
106 present in both <literal>a</literal> and <literal>b</literal> arrays. Values are
107 sorted in increasing order
110 for complex numbers : par increasing magnitudes, then by increasing phases
113 for text : in alphabetical order.
118 Two NaN elements are always considered as different. So NaN or rows or columns having
119 NaN will never be in the result <literal>M</literal>.
122 <literal>[M, ka, kb] = intersect(a,b)</literal> additionally returns the vectors
123 <literal>ka</literal> and <literal>kb</literal> of indices in <literal>a</literal>
124 and <literal>b</literal> of selected components firstly met, such that
125 <literal>M=a(ka)</literal> and <literal>M=b(kb)</literal>.
128 <title>Common rows or columns</title>
130 When the <varname>orient</varname> argument is provided, the comparison is performed
131 between the rows of <literal>a</literal> and <literal>b</literal> -- each one being
132 considered as a whole --, or between their columns.
135 <literal>intersect(a,b,"r")</literal> or <literal>intersect(a,b,1)</literal> will return
136 the matrix of stacked unduplicated rows met in both <literal>a</literal> and
137 <literal>b</literal>, sorted in lexicographic ascending order.
138 If <literal>a</literal> and <literal>b</literal> don't have the same number of columns,
139 [] is returned without comparing the values.
142 <literal>[M,ka,kb] = intersect(a,b,"r")</literal> additionally returns the vectors
143 <literal>ka</literal> and <literal>kb</literal> of the minimal indices of common rows,
144 respectively in <literal>a</literal> and <literal>b</literal>,
145 such that <literal>M=a(ka,:)</literal> and <literal>M=b(kb,:)</literal>.
148 <literal>intersect(a,b,"c")</literal> or <literal>intersect(a,b,2)</literal> does
149 the same for columns.
154 <title>Examples</title>
155 <programlisting role="example"><![CDATA[
156 A = grand(3, 3, "uin", 0, 9)
157 B = grand(2, 4, "uin", 0, 9)
159 [N, ka, kb] = intersect(A,B);
163 --> A = grand(3, 3, "uin", 0, 9)
169 --> B = grand(2, 4, "uin", 0, 9)
178 --> [N, ka, kb] = intersect(A,B);
186 In the above example, note that 6 is met four times in A, at indices [2 4 5 8].
187 Only the minimal index 2 is returned in ka. Same situation for 2 in B.
191 NaN values can never be in the result:
193 <programlisting role="example"><![CDATA[
195 intersect([1 -2 %nan 3 6], [%nan 1:3])
202 --> intersect([1 -2 %nan 3 6], [%nan 1:3])
208 intersect() can also process some characters or some texts. Since Scilab is great with UTF
209 characters, here is an example with some Arabic contents, getting characters present in
212 <programlisting role="example"><![CDATA[
213 A = strsplit("هو برنامج علمي كبير ""Scilab""")'
214 B = strsplit("فهو حر ومفتوح")'
218 --> A = strsplit("هو برنامج علمي كبير ""Scilab""")'
220 !ه و ب ر ن ا م ج ع ل م ي ك ب ي ر " S c i l a b " !
222 --> B = strsplit("فهو حر ومفتوح")'
224 !ف ه و ح ر و م ف ت و ح !
232 Column-wise or Row-wise processing of two matrices: Here we process 2 matrices of
233 signed 1-byte integers, and get the common columns:
235 <programlisting role="example"><![CDATA[
236 A = int8(grand(3,5,"uin",0,1))
237 B = int8(grand(3,9,"uin",0,1))
238 [M,ka,kb] = intersect(A, B, "c");
242 --> A = int8(grand(3,5,"uin",0,1))
248 --> B = int8(grand(3,9,"uin",0,1))
254 --> [M,ka,kb] = intersect(A, B, "c");
269 <literal>intersect()</literal> for booleans is mainly useful with the "r" or "c" option.
270 Here is an example with a sparse boolean matrix:
272 <programlisting role="example"><![CDATA[
274 A = [F F T F T F ; T F F T T T ; T T F T F F]
275 B = [F T F T F F ; T F F F T F ; F T F F T F]
276 [M,ka,kb] = intersect(A, sparse(B), "c");
277 issparse(M), full(M), ka, kb
280 --> A = [F F T F T F ; T F F T T T ; T T F T F F]
286 --> B = [F T F T F F ; T F F F T F ; F T F F T F]
292 --> [M,ka,kb] = intersect(A, sparse(B), "c");
293 --> issparse(M), full(M), ka, kb
309 <refsection role="see also">
310 <title>See also</title>
311 <simplelist type="inline">
313 <link linkend="members">members</link>
316 <link linkend="unique">unique</link>
319 <link linkend="gsort">gsort</link>
322 <link linkend="union">union</link>
326 <refsection role="history">
327 <title>History</title>
330 <revnumber>6.1.0</revnumber>
332 Complex numbers are now accepted.
336 <revnumber>6.1.1</revnumber>
338 Sparse matrices are now accepted (numbers or booleans).