Mercurial > hg > octave-lyh
comparison 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 |
comparison
equal
deleted
inserted
replaced
7651:443a8f5a50fd | 7652:b5731e43283a |
---|---|
117 s = s(:); | 117 s = s(:); |
118 endif | 118 endif |
119 ## Do the actual work. | 119 ## Do the actual work. |
120 if (isempty (a) || isempty (s)) | 120 if (isempty (a) || isempty (s)) |
121 tf = zeros (size (a), "logical"); | 121 tf = zeros (size (a), "logical"); |
122 a_idx = []; | 122 a_idx = zeros (size (a)); |
123 elseif (numel (s) == 1) | 123 elseif (numel (s) == 1) |
124 tf = (a == s); | 124 tf = (a == s); |
125 a_idx = double (tf); | 125 a_idx = double (tf); |
126 elseif (numel (a) == 1) | 126 elseif (numel (a) == 1) |
127 f = find (a == s, 1); | 127 f = find (a == s, 1); |
163 lt = numel(s); | 163 lt = numel(s); |
164 [s, sidx] = sort (s); | 164 [s, sidx] = sort (s); |
165 [v, p] = sort ([s(2:lt)(:); a(:)]); | 165 [v, p] = sort ([s(2:lt)(:); a(:)]); |
166 idx(p) = cumsum (p <= lt-1) + 1; | 166 idx(p) = cumsum (p <= lt-1) + 1; |
167 idx = idx(lt:end); | 167 idx = idx(lt:end); |
168 tf = (a == reshape (s (idx), size (a))); | 168 tf = (a == reshape (s(idx), size (a))); |
169 a_idx = zeros (size(tf)); | 169 a_idx = zeros (size (tf)); |
170 a_idx(tf) = sidx(idx(tf)); | 170 a_idx(tf) = sidx(idx(tf)); |
171 endif | 171 endif |
172 ## Resize result to the original size of 'a' | 172 ## Resize result to the original size of 'a' |
173 size_a = size(a); | 173 size_a = size (a); |
174 tf = reshape (tf, size_a); | 174 tf = reshape (tf, size_a); |
175 a_idx = reshape (a_idx, size_a); | 175 a_idx = reshape (a_idx, size_a); |
176 endif | 176 endif |
177 endif | 177 endif |
178 else | 178 else |
198 endif | 198 endif |
199 if (iscellstr (a) && iscellstr (s)) | 199 if (iscellstr (a) && iscellstr (s)) |
200 ## Do the actual work | 200 ## Do the actual work |
201 if (isempty (a) || isempty (s)) | 201 if (isempty (a) || isempty (s)) |
202 tf = zeros (size (a), "logical"); | 202 tf = zeros (size (a), "logical"); |
203 a_idx = []; | 203 a_idx = zeros (size (a)); |
204 elseif (numel (s) == 1) | 204 elseif (numel (s) == 1) |
205 tf = strcmp (a, s); | 205 tf = strcmp (a, s); |
206 a_idx = double (tf); | 206 a_idx = double (tf); |
207 elseif (numel (a) == 1) | 207 elseif (numel (a) == 1) |
208 f = find (strcmp (a, s), 1); | 208 f = find (strcmp (a, s), 1); |
255 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])); | 255 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])); |
256 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])); | 256 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])); |
257 %!assert (ismember ("1", "0123456789."), true); | 257 %!assert (ismember ("1", "0123456789."), true); |
258 | 258 |
259 %!test | 259 %!test |
260 %! [result, a_idx] = ismember ([1, 2], []); | |
261 %! assert (result, logical ([0, 0])) | |
262 %! assert (a_idx, [0, 0]); | |
263 | |
264 %!test | |
265 %! [result, a_idx] = ismember ([], [1, 2]); | |
266 %! assert (result, logical ([])) | |
267 %! assert (a_idx, []); | |
268 | |
269 %!test | |
270 %! [result, a_idx] = ismember ({'a', 'b'}, ''); | |
271 %! assert (result, logical ([0, 0])) | |
272 %! assert (a_idx, [0, 0]); | |
273 | |
274 %!test | |
275 %! [result, a_idx] = ismember ({'a', 'b'}, {}); | |
276 %! assert (result, logical ([0, 0])) | |
277 %! assert (a_idx, [0, 0]); | |
278 | |
279 %!test | |
280 %! [result, a_idx] = ismember ('', {'a', 'b'}); | |
281 %! assert (result, false) | |
282 %! assert (a_idx, 0); | |
283 | |
284 %!test | |
285 %! [result, a_idx] = ismember ({}, {'a', 'b'}); | |
286 %! assert (result, logical ([])) | |
287 %! assert (a_idx, []); | |
288 | |
289 %!test | |
260 %! [result, a_idx] = ismember([1 2 3 4 5], [3]); | 290 %! [result, a_idx] = ismember([1 2 3 4 5], [3]); |
261 %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0])); | 291 %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0])); |
262 | 292 |
263 %!test | 293 %!test |
264 %! [result, a_idx] = ismember([1 6], [1 2 3 4 5 1 6 1]); | 294 %! [result, a_idx] = ismember([1 6], [1 2 3 4 5 1 6 1]); |