Mercurial > octave-nkf
comparison scripts/set/intersect.m @ 11922:746f13936eee release-3-0-x
improve set functions for Matlab compatibility
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 16 Jan 2009 08:10:28 +0100 |
parents | a1dbe9d80eee |
children | cadc73247d65 |
comparison
equal
deleted
inserted
replaced
11921:166a195399f7 | 11922:746f13936eee |
---|---|
1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle | 1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle |
2 ## Copyright (C) 2008 Jaroslav Hajek | |
2 ## | 3 ## |
3 ## This file is part of Octave. | 4 ## This file is part of Octave. |
4 ## | 5 ## |
5 ## Octave is free software; you can redistribute it and/or modify it | 6 ## 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 ## under the terms of the GNU General Public License as published by |
28 ## @code{b(ib)==c}. | 29 ## @code{b(ib)==c}. |
29 ## | 30 ## |
30 ## @end deftypefn | 31 ## @end deftypefn |
31 ## @seealso{unique, union, setxor, setdiff, ismember} | 32 ## @seealso{unique, union, setxor, setdiff, ismember} |
32 | 33 |
33 function [c, ia, ib] = intersect (a, b) | 34 function [c, ia, ib] = intersect (a, b, varargin) |
34 if (nargin != 2) | 35 |
36 if (nargin < 2 || nargin > 3) | |
35 print_usage (); | 37 print_usage (); |
36 endif | 38 endif |
39 | |
40 if (nargin == 3 && ! strcmpi (varargin{1}, "rows")) | |
41 error ("intersect: if a third input argument is present, it must be the string 'rows'"); | |
42 endif | |
43 | |
37 | 44 |
38 if (isempty (a) || isempty (b)) | 45 if (isempty (a) || isempty (b)) |
39 c = ia = ib = []; | 46 c = ia = ib = []; |
40 else | 47 else |
41 ## form a and b into sets | 48 ## form a and b into sets |
42 [a, ja] = unique (a); | 49 if (nargout > 1) |
43 [b, jb] = unique (b); | 50 [a, ja] = unique (a, varargin{:}); |
44 | 51 [b, jb] = unique (b, varargin{:}); |
45 c = [a(:); b(:)]; | |
46 [c, ic] = sort (c); ## [a(:);b(:)](ic) == c | |
47 | |
48 if (iscellstr (c)) | |
49 ii = find (strcmp (c(1:end-1), c(2:end))); | |
50 else | |
51 ii = find (c(1:end-1) == c(2:end)); | |
52 endif | 52 endif |
53 | 53 |
54 c = c(ii); ## The answer | 54 if (nargin > 2) |
55 ia = ja(ic(ii)); ## a(ia) == c | 55 c = [a; b]; |
56 ib = jb(ic(ii+1) - length (a)); ## b(ib) == c | 56 [c, ic] = sortrows (c); |
57 ii = find (all (c(1:end-1,:) == c(2:end,:), 2)); | |
58 c = c(ii,:); | |
59 else | |
60 c = [a(:); b(:)]; | |
61 [c, ic] = sort (c); ## [a(:);b(:)](ic) == c | |
62 if (iscellstr (c)) | |
63 ii = find (strcmp (c(1:end-1), c(2:end))); | |
64 else | |
65 ii = find (c(1:end-1) == c(2:end)); | |
66 endif | |
67 c = c(ii); | |
68 endif | |
57 | 69 |
58 | 70 |
59 if (size (b, 1) == 1 || size (a, 1) == 1) | 71 if (nargout > 1) |
72 ia = ja(ic(ii)); ## a(ia) == c | |
73 ib = jb(ic(ii+1) - length (a)); ## b(ib) == c | |
74 endif | |
75 | |
76 | |
77 if (nargin == 2 && (size (b, 1) == 1 || size (a, 1) == 1)) | |
60 c = c.'; | 78 c = c.'; |
61 endif | 79 endif |
62 endif | 80 endif |
63 | 81 |
64 endfunction | 82 endfunction |
72 %! assert( c,[1 3 5 7]); | 90 %! assert( c,[1 3 5 7]); |
73 %! assert(ia,[8 1 7 5]); | 91 %! assert(ia,[8 1 7 5]); |
74 %! assert(ib,[5 1 2 6]); | 92 %! assert(ib,[5 1 2 6]); |
75 %! assert(a(ia),c); | 93 %! assert(a(ia),c); |
76 %! assert(b(ib),c); | 94 %! assert(b(ib),c); |
95 %!test | |
96 %! a = [1,1,2;1,4,5;2,1,7]; | |
97 %! b = [1,4,5;2,3,4;1,1,2;9,8,7]; | |
98 %! [c,ia,ib] = intersect(a,b,'rows'); | |
99 %! assert(c,[1,1,2;1,4,5]); | |
100 %! assert(ia,[1;2]); | |
101 %! assert(ib,[3;1]); | |
102 %! assert(a(ia,:),c); | |
103 %! assert(b(ib,:),c); |