2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2303
|
19 |
3368
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} intersection (@var{x}, @var{y}) |
|
22 ## Return the set of elements that are in both sets @var{x} and @var{y}. |
|
23 ## For example, |
3426
|
24 ## |
3368
|
25 ## @example |
|
26 ## @group |
|
27 ## intersection ([ 1, 2, 3 ], [ 2, 3, 5 ]) |
|
28 ## @result{} [ 2, 3 ] |
|
29 ## @end group |
|
30 ## @end example |
3408
|
31 ## @end deftypefn |
5053
|
32 ## |
3405
|
33 ## @seealso{create_set, union, and complement} |
3368
|
34 |
2314
|
35 ## Author: jwe |
|
36 |
2311
|
37 function y = intersection(a,b) |
559
|
38 |
|
39 if (nargin != 2) |
904
|
40 usage ("intersection(a,b)"); |
559
|
41 endif |
|
42 |
|
43 if(isempty(a) || isempty(b)) |
|
44 y = []; |
|
45 return; |
|
46 endif |
|
47 |
|
48 a = create_set(a); |
|
49 b = create_set(b); |
|
50 |
|
51 if(length(a) < length(b)) |
|
52 yindex = 1; |
|
53 y = zeros(1,length(a)); |
|
54 for index = 1:length(a) |
|
55 if(any(b == a(index))) |
|
56 y(yindex++) = a(index); |
|
57 endif |
|
58 endfor |
|
59 else |
|
60 yindex = 1; |
|
61 y = zeros(1,length(b)); |
|
62 for index = 1:length(b) |
|
63 if(any(a == b(index))) |
|
64 y(yindex++) = b(index); |
|
65 endif |
|
66 endfor |
|
67 endif |
|
68 |
|
69 y = y(1:(yindex-1)); |
|
70 |
|
71 endfunction |