annotate scripts/set/ismember.m @ 7077:525cd5f47ab6

[project @ 2007-10-29 19:12:28 by jwe]
author jwe
date Mon, 29 Oct 2007 19:12:28 +0000
parents 609fd2045523
children 73308b8f8777
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2000, 2005, 2006, 2007 Paul Kienzle
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
2 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
3 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
4 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6730
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6730
diff changeset
8 ## your option) any later version.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
9 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
13 ## General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
14 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6730
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6730
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
18
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
20 ## @deftypefn {Function File} {} ismember (@var{A}, @var{S})
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
21 ## Return a matrix the same shape as @var{A} which has 1 if
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
22 ## @code{A(i,j)} is in @var{S} or 0 if it isn't.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5435
diff changeset
23 ## @seealso{unique, union, intersection, setxor, setdiff}
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
24 ## @end deftypefn
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
25
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
26 ## Author: Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
27 ## Adapted-by: jwe
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
28
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
29 function c = ismember (a, S)
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
30
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
31 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
32 print_usage ();
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
33 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
34
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
35 if (isempty (a) || isempty (S))
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
36 c = zeros (size (a), "logical");
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
37 else
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
38 if (iscell (a) && ! iscell (S))
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
39 tmp{1} = S;
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
40 S = tmp;
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
41 endif
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
42 if (! iscell (a) && iscell (S))
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
43 tmp{1} = a;
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
44 a = tmp;
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
45 endif
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
46 S = unique (S(:));
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
47 lt = length (S);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
48 if (lt == 1)
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
49 if (iscell (a) || iscell (S))
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
50 c = cellfun ("length", a) == cellfun ("length", S);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
51 idx = find (c);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
52 if (isempty (idx))
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
53 c = zeros (size (a), "logical");
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
54 else
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
55 c(idx) = all (char (a(idx)) == repmat (char (S), length (idx), 1), 2);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
56 endif
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
57 else
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
58 c = (a == S);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
59 endif
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
60 elseif (numel (a) == 1)
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
61 if (iscell (a) || iscell (S))
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
62 c = cellfun ("length", a) == cellfun ("length", S);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
63 idx = find (c);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
64 if (isempty (idx))
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
65 c = zeros (size (a), "logical");
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
66 else
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
67 c(idx) = all (repmat (char (a), length (idx), 1) == char (S(idx)), 2);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
68 c = any(c);
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
69 endif
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
70 else
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
71 c = any (a == S);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
72 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
73 else
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
74 ## Magic: the following code determines for each a, the index i
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
75 ## such that S(i)<= a < S(i+1). It does this by sorting the a
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
76 ## into S and remembering the source index where each element came
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
77 ## from. Since all the a's originally came after all the S's, if
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
78 ## the source index is less than the length of S, then the element
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
79 ## came from S. We can then do a cumulative sum on the indices to
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
80 ## figure out which element of S each a comes after.
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
81 ## E.g., S=[2 4 6], a=[1 2 3 4 5 6 7]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
82 ## unsorted [S a] = [ 2 4 6 1 2 3 4 5 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
83 ## sorted [ S a ] = [ 1 2 2 3 4 4 5 6 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
84 ## source index p = [ 4 1 5 6 2 7 8 3 9 10 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
85 ## boolean p<=l(S) = [ 0 1 0 0 1 0 0 1 0 0 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
86 ## cumsum(p<=l(S)) = [ 0 1 1 1 2 2 2 3 3 3 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
87 ## Note that this leaves a(1) coming after S(0) which doesn't
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
88 ## exist. So arbitrarily, we will dump all elements less than
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
89 ## S(1) into the interval after S(1). We do this by dropping S(1)
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
90 ## from the sort! E.g., S=[2 4 6], a=[1 2 3 4 5 6 7]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
91 ## unsorted [S(2:3) a] =[4 6 1 2 3 4 5 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
92 ## sorted [S(2:3) a] = [ 1 2 3 4 4 5 6 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
93 ## source index p = [ 3 4 5 1 6 7 2 8 9 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
94 ## boolean p<=l(S)-1 = [ 0 0 0 1 0 0 1 0 0 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
95 ## cumsum(p<=l(S)-1) = [ 0 0 0 1 1 1 2 2 2 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
96 ## Now we can use Octave's lvalue indexing to "invert" the sort,
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
97 ## and assign all these indices back to the appropriate A and S,
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
98 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
99 ## a_idx, and we know which interval S(i) contains a. It is
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
100 ## easy to now check membership by comparing S(a_idx) == a. This
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
101 ## magic works because S starts out sorted, and because sort
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
102 ## preserves the relative order of identical elements.
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
103 [v, p] = sort ([S(2:lt); a(:)]);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
104 idx(p) = cumsum (p <= lt-1) + 1;
6240
3e5a3b5fd40e [project @ 2007-01-11 07:32:12 by jwe]
jwe
parents: 6046
diff changeset
105 idx = idx(lt:end);
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
106 if (iscell (a) || iscell (S))
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
107 c = (cellfun ("length", a)
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
108 == reshape (cellfun ("length", S(idx)), size (a)));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
109 idx2 = find (c);
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
110 c(idx2) = all (char (a(idx2)) == char (S(idx)(idx2)), 2);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
111 else
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
112 c = (a == reshape (S (idx), size (a)));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
113 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
114 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
115 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
116
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
117 endfunction
6533
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
118
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
119 %!assert (ismember ({''}, {'abc', 'def'}), false);
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
120 %!assert (ismember ('abc', {'abc', 'def'}), true);
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
121 %!assert (isempty (ismember ([], [1, 2])), true);
7077
525cd5f47ab6 [project @ 2007-10-29 19:12:28 by jwe]
jwe
parents: 7068
diff changeset
122 %!assert (isempty (ismember ({}, {'a', 'b'})), true);
6730
230f9c49857e [project @ 2007-06-15 04:22:13 by jwe]
jwe
parents: 6664
diff changeset
123 %!xtest assert (ismember ('', {'abc', 'def'}), false);
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
124 %!xtest fail ('ismember ([], {1, 2})', 'error:.*');
6664
c6e7b03af779 [project @ 2007-05-24 21:59:12 by jwe]
jwe
parents: 6609
diff changeset
125 %!fail ('ismember ({[]}, {1, 2})', 'error:.*');
7077
525cd5f47ab6 [project @ 2007-10-29 19:12:28 by jwe]
jwe
parents: 7068
diff changeset
126 %!xtest fail ('ismember ({}, {1, 2})', 'error:.*');
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
127 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0]))
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
128 %!assert (ismember ({'foo'}, {'foobar'}), false)
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
129 %!assert (ismember ({'bar'}, {'foobar'}), false)
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
130 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true)
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
131 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1]))
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
132 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1]))
7068
609fd2045523 [project @ 2007-10-25 07:10:20 by jwe]
jwe
parents: 7067
diff changeset
133 %!assert (ismember ("1", "0123456789."), true)
7077
525cd5f47ab6 [project @ 2007-10-29 19:12:28 by jwe]
jwe
parents: 7068
diff changeset
134 %!assert (ismember ("1.1", "0123456789."), logical ([1, 1, 1]))