0
|
1 %## Copyright (C) 2000-2012 Paul Kienzle
|
|
2 %## Copyright (C) 2008-2009 Jaroslav Hajek
|
|
3 %##
|
|
4 %## This file is part of Octave.
|
|
5 %##
|
|
6 %## Octave is free software; you can redistribute it and/or modify it
|
|
7 %## under the terms of the GNU General Public License as published by
|
|
8 %## the Free Software Foundation; either version 3 of the License, or (at
|
|
9 %## your option) any later version.
|
|
10 %##
|
|
11 %## Octave is distributed in the hope that it will be useful, but
|
|
12 %## WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 %## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ...
|
|
14 % GNU
|
|
15 %## General Public License for more details.
|
|
16 %##
|
|
17 %## You should have received a copy of the GNU General Public ...
|
|
18 % License
|
|
19 %## along with Octave; see the file COPYING. If not, see
|
|
20 %## <http://www.gnu.org/licenses/>.
|
|
21
|
|
22 function [c, ia, ib] = intersect (a, b, varargin)
|
|
23
|
|
24 if (nargin < 2 || nargin > 3)
|
|
25 print_usage ();
|
|
26 end
|
|
27
|
|
28
|
|
29 if (isempty (a) || isempty (b))
|
|
30 c = ia = ib = [];
|
|
31 else
|
|
32 ## form a and b into sets
|
|
33 if (nargout > 1)
|
|
34 [a, ja] = unique (a, varargin{:});
|
|
35 [b, jb] = unique (b, varargin{:});
|
|
36 else
|
|
37 a = unique (a, varargin{:});
|
|
38 b = unique (b, varargin{:});
|
|
39 end
|
|
40
|
|
41 if (nargin > 2)
|
|
42 c = [a; b];
|
|
43 [c, ic] = sortrows (c);
|
|
44 ii = find (all (c(1:end-1,:) == c(2:end,:), 2));
|
|
45 c = c(ii,:);
|
|
46 len_a = rows (a);
|
|
47 else
|
|
48 c = [a(:); b(:)];
|
|
49 [c, ic] = sort (c); ## [a(:);b(:)](ic) == c
|
|
50 if (iscellstr (c))
|
|
51 ii = find (strcmp (c(1:end-1), c(2:end)));
|
|
52 else
|
|
53 ii = find (c(1:end-1) == c(2:end));
|
|
54 end
|
|
55 c = c(ii);
|
|
56 len_a = length (a);
|
|
57 end
|
|
58
|
|
59 if (nargout > 1)
|
|
60 ia = ja(ic(ii)); ## a(ia) == c
|
|
61 ib = jb(ic(ii+1) - len_a); ## b(ib) == c
|
|
62 end
|
|
63
|
|
64 if (nargin == 2 && (size (b, 1) == 1 || size (a, 1) == 1))
|
|
65 c = c.';
|
|
66 end
|
|
67 end
|