annotate scripts/set/ismember.m @ 7067:88417316c1b0

[project @ 2007-10-25 06:57:16 by jwe]
author jwe
date Thu, 25 Oct 2007 06:57:17 +0000
parents a1dbe9d80eee
children 609fd2045523
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 -*-
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
20 ## @deftypefn {Function File} [@var{bool}, @var{index}] = ismember (@var{a}, @var{s})
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
21 ## Return a matrix @var{bool} the same shape as @var{a} which has 1 if
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
22 ## @code{a(i,j)} is in @var{s} or 0 if it isn't. If a second output argument
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
23 ## is requested, the indexes into @var{s} of the matching elements is
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
24 ## also returned.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5435
diff changeset
25 ## @seealso{unique, union, intersection, setxor, setdiff}
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
27
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
28 ## Author: Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
29 ## Adapted-by: jwe
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
30
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
31 function [c, index] = ismember (a, s)
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
32
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
33 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
34 print_usage ();
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
35 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
36
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
37 ## Convert char matrices to cell arrays.
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
38 if (ischar (a))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
39 a = cellstr (a);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
40 endif
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
41 if (ischar (s))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
42 s = cellstr (s);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
43 endif
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
44
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
45 ## Input checking.
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
46 if (! isa (a, class (s)))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
47 error ("ismember: both input arguments must be the same type");
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
48 endif
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
49
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
50 if (iscell (a) && ! iscellstr (a))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
51 error ("ismember: cell arrays may only contain strings");
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
52 endif
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
53
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
54 if (! isnumeric(a) && ! iscell (a))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
55 error ("ismember: input arguments must be arrays, cell arrays, or strings");
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
56 endif
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
57
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
58 ## Do the actual work.
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
59 if (isempty (a) || isempty (s))
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
60 c = zeros (size (a), "logical");
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
61 else
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
62 if (numel (s) == 1)
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
63 if (iscell (a))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
64 c = strcmp (a, s);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
65 else
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
66 ## Both A and S are matrices.
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
67 c = (a == s);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
68 endif
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
69 index = double (c);
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
70 elseif (numel (a) == 1)
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
71 if (iscell (a))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
72 f = find (strcmp (a, s), 1);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
73 else
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
74 ## Both A and S are matrices.
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
75 f = find (a == s, 1);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
76 endif
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
77 c = ! isempty (f);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
78 index = f;
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
79 if (isempty (index))
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
80 index = 0;
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
81 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
82 else
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
83 ## 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
84 ## 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
85 ## 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
86 ## 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
87 ## 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
88 ## 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
89 ## figure out which element of S each a comes after.
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
90 ## 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 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
92 ## 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
93 ## 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
94 ## 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
95 ## 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
96 ## 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
97 ## exist. So arbitrarily, we will dump all elements less than
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
98 ## 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
99 ## 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
100 ## 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
101 ## 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
102 ## 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
103 ## 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
104 ## 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
105 ## 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
106 ## 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
107 ## 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
108 ## 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
109 ## 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
110 ## magic works because S starts out sorted, and because sort
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
111 ## preserves the relative order of identical elements.
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
112 lt = length (s);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
113 [s, sidx] = sort (s);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
114 [v, p] = sort ([s(2:lt); a(:)]);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
115 idx(p) = cumsum (p <= lt-1) + 1;
6240
3e5a3b5fd40e [project @ 2007-01-11 07:32:12 by jwe]
jwe
parents: 6046
diff changeset
116 idx = idx(lt:end);
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
117 if (iscell (a) || iscell (s))
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
118 c = (cellfun ("length", a)
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
119 == reshape (cellfun ("length", s(idx)), size (a)));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
120 idx2 = find (c);
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
121 c(idx2) = all (char (a(idx2)) == char (s(idx)(idx2)), 2);
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
122 index = zeros (size (c));
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
123 index(c) = sidx(idx(c));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
124 else
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
125 ## Both A and S are matrices.
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
126 c = (a == reshape (s (idx), size (a)));
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
127 index = zeros (size (c));
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
128 index(c) = sidx(idx(c));
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
129 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
130 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
131 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
132
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
133 endfunction
6533
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
134
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
135 %!assert (ismember ({''}, {'abc', 'def'}), false);
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
136 %!assert (ismember ('abc', {'abc', 'def'}), true);
c64687e6f1c3 [project @ 2007-04-17 21:21:46 by jwe]
jwe
parents: 6240
diff changeset
137 %!assert (isempty (ismember ([], [1, 2])), true);
6730
230f9c49857e [project @ 2007-06-15 04:22:13 by jwe]
jwe
parents: 6664
diff changeset
138 %!xtest assert (ismember ('', {'abc', 'def'}), false);
7067
88417316c1b0 [project @ 2007-10-25 06:57:16 by jwe]
jwe
parents: 7017
diff changeset
139 %!fail ('ismember ([], {1, 2})', 'error:.*');
6664
c6e7b03af779 [project @ 2007-05-24 21:59:12 by jwe]
jwe
parents: 6609
diff changeset
140 %!fail ('ismember ({[]}, {1, 2})', 'error:.*');
6609
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
141 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0]))
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
142 %!assert (ismember ({'foo'}, {'foobar'}), false)
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
143 %!assert (ismember ({'bar'}, {'foobar'}), false)
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
144 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true)
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
145 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1]))
30891d1d0c86 [project @ 2007-05-09 02:12:04 by jwe]
jwe
parents: 6533
diff changeset
146 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1]))