Mercurial > octave
annotate scripts/set/ismember.m @ 7652:b5731e43283a
ismember: correctly size idx output for empty args
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 26 Mar 2008 23:01:16 -0400 |
parents | 363ffc8a5c80 |
children | 3092dd54ad95 72830070a17b |
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 ## | |
5642 | 63 ## @seealso{unique, union, intersection, 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)) | |
187 if (isempty (a)) # Work around bug in 'cellstr' | |
188 a = {''}; | |
189 else | |
7129 | 190 a = cellstr (a); |
7128 | 191 endif |
192 elseif (iscellstr (a) && ischar (s)) | |
193 if (isempty (s)) # Work around bug in 'cellstr' | |
194 s = {''}; | |
5205 | 195 else |
7129 | 196 s = cellstr (s); |
5205 | 197 endif |
7128 | 198 endif |
199 if (iscellstr (a) && iscellstr (s)) | |
200 ## Do the actual work | |
201 if (isempty (a) || isempty (s)) | |
202 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
|
203 a_idx = zeros (size (a)); |
7128 | 204 elseif (numel (s) == 1) |
205 tf = strcmp (a, s); | |
206 a_idx = double (tf); | |
207 elseif (numel (a) == 1) | |
208 f = find (strcmp (a, s), 1); | |
209 tf = !isempty (f); | |
210 a_idx = f; | |
211 if (isempty (a_idx)) | |
212 a_idx = 0; | |
213 endif | |
214 else | |
7129 | 215 lt = numel (s); |
7128 | 216 [s, sidx] = sort (s); |
217 [v, p] = sort ([s(2:lt)(:); a(:)]); | |
218 idx(p) = cumsum (p <= lt-1) + 1; | |
219 idx = idx(lt:end); | |
220 tf = (cellfun ("length", a) | |
7129 | 221 == reshape (cellfun ("length", s(idx)), size (a))); |
7128 | 222 idx2 = find (tf); |
7129 | 223 tf(idx2) = (all (char (a(idx2)) == char (s(idx)(idx2)), 2)); |
7128 | 224 a_idx = zeros (size (tf)); |
225 a_idx(tf) = sidx(idx(tf)); | |
5205 | 226 endif |
5178 | 227 else |
7128 | 228 error ("cell_ismember: arguments must be cell arrays of character strings"); |
5178 | 229 endif |
7128 | 230 else |
231 print_usage (); | |
5178 | 232 endif |
7128 | 233 ## Resize result to the original size of 'a' |
7129 | 234 size_a = size (a); |
7128 | 235 tf = reshape (tf, size_a); |
236 a_idx = reshape (a_idx, size_a); | |
5178 | 237 endfunction |
6533 | 238 |
239 %!assert (ismember ({''}, {'abc', 'def'}), false); | |
240 %!assert (ismember ('abc', {'abc', 'def'}), true); | |
241 %!assert (isempty (ismember ([], [1, 2])), true); | |
7077 | 242 %!assert (isempty (ismember ({}, {'a', 'b'})), true); |
7128 | 243 %!assert (ismember ('', {'abc', 'def'}), false); |
244 %!fail ('ismember ([], {1, 2})', 'error:.*'); | |
6664 | 245 %!fail ('ismember ({[]}, {1, 2})', 'error:.*'); |
7128 | 246 %!fail ('ismember ({}, {1, 2})', 'error:.*'); |
247 %!fail ('ismember ({1}, {''1'', ''2''})', 'error:.*'); | |
248 %!fail ('ismember (1, ''abc'')', 'error:.*'); | |
249 %!fail ('ismember ({''1''}, {''1'', ''2''},''rows'')', 'error:.*'); | |
250 %!fail ('ismember ([1 2 3], [5 4 3 1], ''rows'')', 'error:.*'); | |
251 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0])); | |
252 %!assert (ismember ({'foo'}, {'foobar'}), false); | |
253 %!assert (ismember ({'bar'}, {'foobar'}), false); | |
254 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true); | |
255 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])); | |
256 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])); | |
257 %!assert (ismember ("1", "0123456789."), true); | |
258 | |
259 %!test | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
260 %! [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
|
261 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
262 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
263 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
264 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
265 %! [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
|
266 %! assert (result, logical ([])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
267 %! assert (a_idx, []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
268 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
269 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
270 %! [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
|
271 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
272 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
273 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
274 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
275 %! [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
|
276 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
277 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
278 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
279 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
280 %! [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
|
281 %! assert (result, false) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
282 %! assert (a_idx, 0); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
283 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
284 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
285 %! [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
|
286 %! assert (result, logical ([])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
287 %! assert (a_idx, []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
288 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
289 %!test |
7128 | 290 %! [result, a_idx] = ismember([1 2 3 4 5], [3]); |
291 %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0])); | |
292 | |
293 %!test | |
294 %! [result, a_idx] = ismember([1 6], [1 2 3 4 5 1 6 1]); | |
295 %! assert (all (result == logical ([1 1])) && all (a_idx == [8 7])); | |
296 | |
297 %!test | |
298 %! [result, a_idx] = ismember ([3,10,1], [0,1,2,3,4,5,6,7,8,9]); | |
299 %! assert (all (result == logical ([1, 0, 1])) && all (a_idx == [4, 0, 2])); | |
300 | |
301 %!test | |
302 %! [result, a_idx] = ismember ("1.1", "0123456789.1"); | |
303 %! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [12, 11, 12])); | |
304 | |
305 %!test | |
306 %! [result, a_idx] = ismember([1:3; 5:7; 4:6], [0:2; 1:3; 2:4; 3:5; 4:6], 'rows'); | |
307 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [2; 0; 5])); | |
308 | |
309 %!test | |
310 %! [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'); | |
311 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [1; 0; 2])); | |
312 |