comparison scripts/set/ismember.m @ 7068:609fd2045523

[project @ 2007-10-25 07:10:20 by jwe]
author jwe
date Thu, 25 Oct 2007 07:10:20 +0000
parents 88417316c1b0
children 525cd5f47ab6
comparison
equal deleted inserted replaced
7067:88417316c1b0 7068:609fd2045523
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} [@var{bool}, @var{index}] = ismember (@var{a}, @var{s}) 20 ## @deftypefn {Function File} {} ismember (@var{A}, @var{S})
21 ## Return a matrix @var{bool} the same shape as @var{a} which has 1 if 21 ## Return a matrix the same shape as @var{A} which has 1 if
22 ## @code{a(i,j)} is in @var{s} or 0 if it isn't. If a second output argument 22 ## @code{A(i,j)} is in @var{S} or 0 if it isn't.
23 ## is requested, the indexes into @var{s} of the matching elements is
24 ## also returned.
25 ## @seealso{unique, union, intersection, setxor, setdiff} 23 ## @seealso{unique, union, intersection, setxor, setdiff}
26 ## @end deftypefn 24 ## @end deftypefn
27 25
28 ## Author: Paul Kienzle 26 ## Author: Paul Kienzle
29 ## Adapted-by: jwe 27 ## Adapted-by: jwe
30 28
31 function [c, index] = ismember (a, s) 29 function c = ismember (a, S)
32 30
33 if (nargin != 2) 31 if (nargin != 2)
34 print_usage (); 32 print_usage ();
35 endif 33 endif
36 34
37 ## Convert char matrices to cell arrays. 35 if (isempty (a) || isempty (S))
38 if (ischar (a))
39 a = cellstr (a);
40 endif
41 if (ischar (s))
42 s = cellstr (s);
43 endif
44
45 ## Input checking.
46 if (! isa (a, class (s)))
47 error ("ismember: both input arguments must be the same type");
48 endif
49
50 if (iscell (a) && ! iscellstr (a))
51 error ("ismember: cell arrays may only contain strings");
52 endif
53
54 if (! isnumeric(a) && ! iscell (a))
55 error ("ismember: input arguments must be arrays, cell arrays, or strings");
56 endif
57
58 ## Do the actual work.
59 if (isempty (a) || isempty (s))
60 c = zeros (size (a), "logical"); 36 c = zeros (size (a), "logical");
61 else 37 else
62 if (numel (s) == 1) 38 if (iscell (a) && ! iscell (S))
63 if (iscell (a)) 39 tmp{1} = S;
64 c = strcmp (a, s); 40 S = tmp;
41 endif
42 if (! iscell (a) && iscell (S))
43 tmp{1} = a;
44 a = tmp;
45 endif
46 S = unique (S(:));
47 lt = length (S);
48 if (lt == 1)
49 if (iscell (a) || iscell (S))
50 c = cellfun ("length", a) == cellfun ("length", S);
51 idx = find (c);
52 if (isempty (idx))
53 c = zeros (size (a), "logical");
54 else
55 c(idx) = all (char (a(idx)) == repmat (char (S), length (idx), 1), 2);
56 endif
65 else 57 else
66 ## Both A and S are matrices. 58 c = (a == S);
67 c = (a == s);
68 endif 59 endif
69 index = double (c);
70 elseif (numel (a) == 1) 60 elseif (numel (a) == 1)
71 if (iscell (a)) 61 if (iscell (a) || iscell (S))
72 f = find (strcmp (a, s), 1); 62 c = cellfun ("length", a) == cellfun ("length", S);
63 idx = find (c);
64 if (isempty (idx))
65 c = zeros (size (a), "logical");
66 else
67 c(idx) = all (repmat (char (a), length (idx), 1) == char (S(idx)), 2);
68 c = any(c);
69 endif
73 else 70 else
74 ## Both A and S are matrices. 71 c = any (a == S);
75 f = find (a == s, 1);
76 endif
77 c = ! isempty (f);
78 index = f;
79 if (isempty (index))
80 index = 0;
81 endif 72 endif
82 else 73 else
83 ## Magic: the following code determines for each a, the index i 74 ## Magic: the following code determines for each a, the index i
84 ## such that S(i)<= a < S(i+1). It does this by sorting the a 75 ## such that S(i)<= a < S(i+1). It does this by sorting the a
85 ## into S and remembering the source index where each element came 76 ## into S and remembering the source index where each element came
107 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to 98 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to
108 ## a_idx, and we know which interval S(i) contains a. It is 99 ## a_idx, and we know which interval S(i) contains a. It is
109 ## easy to now check membership by comparing S(a_idx) == a. This 100 ## easy to now check membership by comparing S(a_idx) == a. This
110 ## magic works because S starts out sorted, and because sort 101 ## magic works because S starts out sorted, and because sort
111 ## preserves the relative order of identical elements. 102 ## preserves the relative order of identical elements.
112 lt = length (s); 103 [v, p] = sort ([S(2:lt); a(:)]);
113 [s, sidx] = sort (s);
114 [v, p] = sort ([s(2:lt); a(:)]);
115 idx(p) = cumsum (p <= lt-1) + 1; 104 idx(p) = cumsum (p <= lt-1) + 1;
116 idx = idx(lt:end); 105 idx = idx(lt:end);
117 if (iscell (a) || iscell (s)) 106 if (iscell (a) || iscell (S))
118 c = (cellfun ("length", a) 107 c = (cellfun ("length", a)
119 == reshape (cellfun ("length", s(idx)), size (a))); 108 == reshape (cellfun ("length", S(idx)), size (a)));
120 idx2 = find (c); 109 idx2 = find (c);
121 c(idx2) = all (char (a(idx2)) == char (s(idx)(idx2)), 2); 110 c(idx2) = all (char (a(idx2)) == char (S(idx)(idx2)), 2);
122 index = zeros (size (c));
123 index(c) = sidx(idx(c));
124 else 111 else
125 ## Both A and S are matrices. 112 c = (a == reshape (S (idx), size (a)));
126 c = (a == reshape (s (idx), size (a)));
127 index = zeros (size (c));
128 index(c) = sidx(idx(c));
129 endif 113 endif
130 endif 114 endif
131 endif 115 endif
132 116
133 endfunction 117 endfunction
134 118
135 %!assert (ismember ({''}, {'abc', 'def'}), false); 119 %!assert (ismember ({''}, {'abc', 'def'}), false);
136 %!assert (ismember ('abc', {'abc', 'def'}), true); 120 %!assert (ismember ('abc', {'abc', 'def'}), true);
137 %!assert (isempty (ismember ([], [1, 2])), true); 121 %!assert (isempty (ismember ([], [1, 2])), true);
138 %!xtest assert (ismember ('', {'abc', 'def'}), false); 122 %!xtest assert (ismember ('', {'abc', 'def'}), false);
139 %!fail ('ismember ([], {1, 2})', 'error:.*'); 123 %!xtest fail ('ismember ([], {1, 2})', 'error:.*');
140 %!fail ('ismember ({[]}, {1, 2})', 'error:.*'); 124 %!fail ('ismember ({[]}, {1, 2})', 'error:.*');
141 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0])) 125 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0]))
142 %!assert (ismember ({'foo'}, {'foobar'}), false) 126 %!assert (ismember ({'foo'}, {'foobar'}), false)
143 %!assert (ismember ({'bar'}, {'foobar'}), false) 127 %!assert (ismember ({'bar'}, {'foobar'}), false)
144 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true) 128 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true)
145 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])) 129 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1]))
146 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])) 130 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1]))
131 %!assert (ismember ("1", "0123456789."), true)