Mercurial > hg > octave-lyh
annotate scripts/set/ismember.m @ 8506:bc982528de11
comment style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 13 Jan 2009 11:56:00 -0500 |
parents | 6f2d95255911 |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2000, 2005, 2006, 2007 Paul Kienzle |
5178 | 2 ## |
5181 | 3 ## This file is part of Octave. |
5178 | 4 ## |
5181 | 5 ## 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5181 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
5178 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5178 | 18 |
19 ## -*- texinfo -*- | |
7128 | 20 ## @deftypefn {Function File} {[@var{tf}, @var{a_idx}] =} ismember (@var{A}, @var{S}) |
7129 | 21 ## @deftypefnx {Function File} {[@var{tf}, @var{a_idx}] =} ismember (@var{A}, @var{S}, "rows") |
7128 | 22 ## Return a matrix @var{tf} the same shape as @var{A} which has 1 if |
23 ## @code{A(i,j)} is in @var{S} or 0 if it isn't. If a second output argument | |
7129 | 24 ## is requested, the indexes into @var{S} of the matching elements are |
7128 | 25 ## also returned. |
26 ## | |
27 ## @example | |
28 ## @group | |
29 ## a = [3, 10, 1]; | |
30 ## s = [0:9]; | |
31 ## [tf, a_idx] = residue (a, s); | |
32 ## @result{} tf = [1, 0, 1] | |
33 ## @result{} a_idx = [4, 0, 2] | |
34 ## @end group | |
35 ## @end example | |
36 ## | |
37 ## The inputs, @var{A} and @var{S}, may also be cell arrays. | |
38 ## | |
39 ## @example | |
40 ## @group | |
41 ## a = @{'abc'@}; | |
42 ## s = @{'abc', 'def'@}; | |
43 ## [tf, a_idx] = residue (a, s); | |
44 ## @result{} tf = [1, 0] | |
45 ## @result{} a_idx = [1, 0] | |
46 ## @end group | |
47 ## @end example | |
48 ## | |
7129 | 49 ## With the optional third argument @code{"rows"}, and matrices |
50 ## @var{A} and @var{S} with the same number of columns, compare rows in | |
51 ## @var{A} with the rows in @var{S}. | |
7128 | 52 ## |
53 ## @example | |
54 ## @group | |
55 ## a = [1:3; 5:7; 4:6]; | |
56 ## s = [0:2; 1:3; 2:4; 3:5; 4:6]; | |
57 ## [tf, a_idx] = ismember(a, s, 'rows'); | |
58 ## @result{} tf = logical ([1; 0; 1]) | |
59 ## @result{} a_idx = [2; 0; 5]; | |
60 ## @end group | |
61 ## @end example | |
62 ## | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7883
diff
changeset
|
63 ## @seealso{unique, union, intersect, setxor, setdiff} |
5178 | 64 ## @end deftypefn |
65 | |
7129 | 66 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
67 ## Author: Søren Hauberg <hauberg@gmail.com> | |
68 ## Author: Ben Abbott <bpabbott@mac.com> | |
5181 | 69 ## Adapted-by: jwe |
70 | |
7128 | 71 function [tf, a_idx] = ismember (a, s, rows_opt) |
5181 | 72 |
7128 | 73 if (nargin == 2 || nargin == 3) |
7129 | 74 if (iscell (a) || iscell (s)) |
7128 | 75 if (nargin == 3) |
76 error ("ismember: with 'rows' both sets must be matrices"); | |
77 else | |
78 [tf, a_idx] = cell_ismember (a, s); | |
79 endif | |
80 else | |
81 if (nargin == 3) | |
82 ## The 'rows' argument is handled in a fairly ugly way. A better | |
83 ## solution would be to vectorize this loop over 'r' below. | |
7129 | 84 if (strcmpi (rows_opt, "rows") && ismatrix (a) && ismatrix (s) |
85 && columns (a) == columns (s)) | |
7128 | 86 rs = rows (s); |
87 ra = rows (a); | |
88 a_idx = zeros (ra, 1); | |
89 for r = 1:ra | |
90 tmp = ones (rs, 1) * a(r,:); | |
91 f = find (all (tmp' == s'), 1); | |
7129 | 92 if (! isempty (f)) |
7128 | 93 a_idx(r) = f; |
94 endif | |
95 endfor | |
96 tf = logical (a_idx); | |
7129 | 97 elseif (strcmpi (rows_opt, "rows")) |
7128 | 98 error ("ismember: with 'rows' both sets must be matrices with an equal number of columns"); |
99 else | |
100 error ("ismember: invalid input"); | |
101 endif | |
102 else | |
103 ## Input checking | |
7129 | 104 if (! isa (a, class (s))) |
7128 | 105 error ("ismember: both input arguments must be the same type"); |
7129 | 106 elseif (! ischar (a) && ! isnumeric (a)) |
7128 | 107 error ("ismember: input arguments must be arrays, cell arrays, or strings"); |
7129 | 108 elseif (ischar (a) && ischar (s)) |
7128 | 109 a = uint8 (a); |
110 s = uint8 (s); | |
111 endif | |
7129 | 112 ## Convert matrices to vectors. |
7128 | 113 if (all (size (a) > 1)) |
114 a = a(:); | |
115 endif | |
116 if (all (size (s) > 1)) | |
117 s = s(:); | |
118 endif | |
7129 | 119 ## Do the actual work. |
7128 | 120 if (isempty (a) || isempty (s)) |
121 tf = zeros (size (a), "logical"); | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
122 a_idx = zeros (size (a)); |
7128 | 123 elseif (numel (s) == 1) |
124 tf = (a == s); | |
125 a_idx = double (tf); | |
126 elseif (numel (a) == 1) | |
127 f = find (a == s, 1); | |
7129 | 128 tf = !isempty (f); |
7128 | 129 a_idx = f; |
130 if (isempty (a_idx)) | |
131 a_idx = 0; | |
132 endif | |
133 else | |
134 ## Magic: the following code determines for each a, the index i | |
135 ## such that s(i)<= a < s(i+1). It does this by sorting the a | |
136 ## into s and remembering the source index where each element came | |
137 ## from. Since all the a's originally came after all the s's, if | |
138 ## the source index is less than the length of s, then the element | |
139 ## came from s. We can then do a cumulative sum on the indices to | |
140 ## figure out which element of s each a comes after. | |
141 ## E.g., s=[2 4 6], a=[1 2 3 4 5 6 7] | |
142 ## unsorted [s a] = [ 2 4 6 1 2 3 4 5 6 7 ] | |
143 ## sorted [s a] = [ 1 2 2 3 4 4 5 6 6 7 ] | |
144 ## source index p = [ 4 1 5 6 2 7 8 3 9 10 ] | |
145 ## boolean p<=l(s) = [ 0 1 0 0 1 0 0 1 0 0 ] | |
146 ## cumsum(p<=l(s)) = [ 0 1 1 1 2 2 2 3 3 3 ] | |
147 ## Note that this leaves a(1) coming after s(0) which doesn't | |
148 ## exist. So arbitrarily, we will dump all elements less than | |
149 ## s(1) into the interval after s(1). We do this by dropping s(1) | |
150 ## from the sort! E.g., s=[2 4 6], a=[1 2 3 4 5 6 7] | |
151 ## unsorted [s(2:3) a] =[4 6 1 2 3 4 5 6 7 ] | |
152 ## sorted [s(2:3) a] = [ 1 2 3 4 4 5 6 6 7 ] | |
153 ## source index p = [ 3 4 5 1 6 7 2 8 9 ] | |
154 ## boolean p<=l(s)-1 = [ 0 0 0 1 0 0 1 0 0 ] | |
155 ## cumsum(p<=l(s)-1) = [ 0 0 0 1 1 1 2 2 2 ] | |
156 ## Now we can use Octave's lvalue indexing to "invert" the sort, | |
157 ## and assign all these indices back to the appropriate a and s, | |
158 ## giving s_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to | |
159 ## a_idx, and we know which interval s(i) contains a. It is | |
160 ## easy to now check membership by comparing s(a_idx) == a. This | |
161 ## magic works because s starts out sorted, and because sort | |
162 ## preserves the relative order of identical elements. | |
163 lt = numel(s); | |
164 [s, sidx] = sort (s); | |
165 [v, p] = sort ([s(2:lt)(:); a(:)]); | |
166 idx(p) = cumsum (p <= lt-1) + 1; | |
167 idx = idx(lt:end); | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
168 tf = (a == reshape (s(idx), size (a))); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
169 a_idx = zeros (size (tf)); |
7128 | 170 a_idx(tf) = sidx(idx(tf)); |
171 endif | |
172 ## Resize result to the original size of 'a' | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
173 size_a = size (a); |
7128 | 174 tf = reshape (tf, size_a); |
175 a_idx = reshape (a_idx, size_a); | |
176 endif | |
177 endif | |
178 else | |
6046 | 179 print_usage (); |
5178 | 180 endif |
181 | |
7128 | 182 endfunction |
183 | |
184 function [tf, a_idx] = cell_ismember (a, s) | |
185 if (nargin == 2) | |
186 if (ischar (a) && iscellstr (s)) | |
8506 | 187 if (isempty (a)) |
188 ## Work around bug in cellstr. | |
7128 | 189 a = {''}; |
190 else | |
7129 | 191 a = cellstr (a); |
7128 | 192 endif |
193 elseif (iscellstr (a) && ischar (s)) | |
8506 | 194 if (isempty (s)) |
195 ## Work around bug in cellstr. | |
7128 | 196 s = {''}; |
5205 | 197 else |
7129 | 198 s = cellstr (s); |
5205 | 199 endif |
7128 | 200 endif |
201 if (iscellstr (a) && iscellstr (s)) | |
8506 | 202 ## Do the actual work. |
7128 | 203 if (isempty (a) || isempty (s)) |
204 tf = zeros (size (a), "logical"); | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
205 a_idx = zeros (size (a)); |
7128 | 206 elseif (numel (s) == 1) |
207 tf = strcmp (a, s); | |
208 a_idx = double (tf); | |
209 elseif (numel (a) == 1) | |
210 f = find (strcmp (a, s), 1); | |
211 tf = !isempty (f); | |
212 a_idx = f; | |
213 if (isempty (a_idx)) | |
214 a_idx = 0; | |
215 endif | |
216 else | |
7129 | 217 lt = numel (s); |
7128 | 218 [s, sidx] = sort (s); |
219 [v, p] = sort ([s(2:lt)(:); a(:)]); | |
220 idx(p) = cumsum (p <= lt-1) + 1; | |
221 idx = idx(lt:end); | |
222 tf = (cellfun ("length", a) | |
7129 | 223 == reshape (cellfun ("length", s(idx)), size (a))); |
7128 | 224 idx2 = find (tf); |
7129 | 225 tf(idx2) = (all (char (a(idx2)) == char (s(idx)(idx2)), 2)); |
7128 | 226 a_idx = zeros (size (tf)); |
227 a_idx(tf) = sidx(idx(tf)); | |
5205 | 228 endif |
5178 | 229 else |
7128 | 230 error ("cell_ismember: arguments must be cell arrays of character strings"); |
5178 | 231 endif |
7128 | 232 else |
233 print_usage (); | |
5178 | 234 endif |
8506 | 235 ## Resize result to the original size of A. |
7129 | 236 size_a = size (a); |
7128 | 237 tf = reshape (tf, size_a); |
238 a_idx = reshape (a_idx, size_a); | |
5178 | 239 endfunction |
6533 | 240 |
241 %!assert (ismember ({''}, {'abc', 'def'}), false); | |
242 %!assert (ismember ('abc', {'abc', 'def'}), true); | |
243 %!assert (isempty (ismember ([], [1, 2])), true); | |
7077 | 244 %!assert (isempty (ismember ({}, {'a', 'b'})), true); |
7128 | 245 %!assert (ismember ('', {'abc', 'def'}), false); |
7883
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
246 %!fail ('ismember ([], {1, 2})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
247 %!fail ('ismember ({[]}, {1, 2})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
248 %!fail ('ismember ({}, {1, 2})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
249 %!fail ('ismember ({1}, {''1'', ''2''})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
250 %!fail ('ismember (1, ''abc'')'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
251 %!fail ('ismember ({''1''}, {''1'', ''2''},''rows'')'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
252 %!fail ('ismember ([1 2 3], [5 4 3 1], ''rows'')'); |
7128 | 253 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0])); |
254 %!assert (ismember ({'foo'}, {'foobar'}), false); | |
255 %!assert (ismember ({'bar'}, {'foobar'}), false); | |
256 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true); | |
257 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])); | |
258 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])); | |
259 %!assert (ismember ("1", "0123456789."), true); | |
260 | |
261 %!test | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
262 %! [result, a_idx] = ismember ([1, 2], []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
263 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
264 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
265 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
266 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
267 %! [result, a_idx] = ismember ([], [1, 2]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
268 %! assert (result, logical ([])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
269 %! assert (a_idx, []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
270 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
271 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
272 %! [result, a_idx] = ismember ({'a', 'b'}, ''); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
273 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
274 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
275 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
276 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
277 %! [result, a_idx] = ismember ({'a', 'b'}, {}); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
278 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
279 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
280 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
281 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
282 %! [result, a_idx] = ismember ('', {'a', 'b'}); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
283 %! assert (result, false) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
284 %! assert (a_idx, 0); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
285 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
286 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
287 %! [result, a_idx] = ismember ({}, {'a', 'b'}); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
288 %! assert (result, logical ([])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
289 %! assert (a_idx, []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
290 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
291 %!test |
7128 | 292 %! [result, a_idx] = ismember([1 2 3 4 5], [3]); |
293 %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0])); | |
294 | |
295 %!test | |
296 %! [result, a_idx] = ismember([1 6], [1 2 3 4 5 1 6 1]); | |
297 %! assert (all (result == logical ([1 1])) && all (a_idx == [8 7])); | |
298 | |
299 %!test | |
300 %! [result, a_idx] = ismember ([3,10,1], [0,1,2,3,4,5,6,7,8,9]); | |
301 %! assert (all (result == logical ([1, 0, 1])) && all (a_idx == [4, 0, 2])); | |
302 | |
303 %!test | |
304 %! [result, a_idx] = ismember ("1.1", "0123456789.1"); | |
305 %! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [12, 11, 12])); | |
306 | |
307 %!test | |
308 %! [result, a_idx] = ismember([1:3; 5:7; 4:6], [0:2; 1:3; 2:4; 3:5; 4:6], 'rows'); | |
309 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [2; 0; 5])); | |
310 | |
311 %!test | |
312 %! [result, a_idx] = ismember([1.1,1.2,1.3; 2.1,2.2,2.3; 10,11,12], [1.1,1.2,1.3; 10,11,12; 2.12,2.22,2.32], 'rows'); | |
313 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [1; 0; 2])); | |
314 |