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 -*- |
7068
|
20 ## @deftypefn {Function File} {} ismember (@var{A}, @var{S}) |
|
21 ## Return a matrix the same shape as @var{A} which has 1 if |
|
22 ## @code{A(i,j)} is in @var{S} or 0 if it isn't. |
5642
|
23 ## @seealso{unique, union, intersection, setxor, setdiff} |
5178
|
24 ## @end deftypefn |
|
25 |
5181
|
26 ## Author: Paul Kienzle |
|
27 ## Adapted-by: jwe |
|
28 |
7068
|
29 function c = ismember (a, S) |
5181
|
30 |
|
31 if (nargin != 2) |
6046
|
32 print_usage (); |
5178
|
33 endif |
|
34 |
7068
|
35 if (isempty (a) || isempty (S)) |
6609
|
36 c = zeros (size (a), "logical"); |
5178
|
37 else |
7068
|
38 if (iscell (a) && ! iscell (S)) |
|
39 tmp{1} = S; |
|
40 S = tmp; |
|
41 endif |
|
42 if (! iscell (a) && iscell (S)) |
|
43 tmp{1} = a; |
|
44 a = tmp; |
|
45 endif |
|
46 S = unique (S(:)); |
|
47 lt = length (S); |
|
48 if (lt == 1) |
|
49 if (iscell (a) || iscell (S)) |
|
50 c = cellfun ("length", a) == cellfun ("length", S); |
|
51 idx = find (c); |
|
52 if (isempty (idx)) |
|
53 c = zeros (size (a), "logical"); |
|
54 else |
|
55 c(idx) = all (char (a(idx)) == repmat (char (S), length (idx), 1), 2); |
|
56 endif |
5205
|
57 else |
7068
|
58 c = (a == S); |
5205
|
59 endif |
6609
|
60 elseif (numel (a) == 1) |
7068
|
61 if (iscell (a) || iscell (S)) |
|
62 c = cellfun ("length", a) == cellfun ("length", S); |
|
63 idx = find (c); |
|
64 if (isempty (idx)) |
|
65 c = zeros (size (a), "logical"); |
|
66 else |
|
67 c(idx) = all (repmat (char (a), length (idx), 1) == char (S(idx)), 2); |
|
68 c = any(c); |
|
69 endif |
5205
|
70 else |
7068
|
71 c = any (a == S); |
5205
|
72 endif |
5178
|
73 else |
5181
|
74 ## Magic: the following code determines for each a, the index i |
5178
|
75 ## such that S(i)<= a < S(i+1). It does this by sorting the a |
|
76 ## into S and remembering the source index where each element came |
|
77 ## from. Since all the a's originally came after all the S's, if |
|
78 ## the source index is less than the length of S, then the element |
|
79 ## came from S. We can then do a cumulative sum on the indices to |
|
80 ## figure out which element of S each a comes after. |
|
81 ## E.g., S=[2 4 6], a=[1 2 3 4 5 6 7] |
|
82 ## unsorted [S a] = [ 2 4 6 1 2 3 4 5 6 7 ] |
|
83 ## sorted [ S a ] = [ 1 2 2 3 4 4 5 6 6 7 ] |
|
84 ## source index p = [ 4 1 5 6 2 7 8 3 9 10 ] |
|
85 ## boolean p<=l(S) = [ 0 1 0 0 1 0 0 1 0 0 ] |
|
86 ## cumsum(p<=l(S)) = [ 0 1 1 1 2 2 2 3 3 3 ] |
|
87 ## Note that this leaves a(1) coming after S(0) which doesn't |
|
88 ## exist. So arbitrarily, we will dump all elements less than |
|
89 ## S(1) into the interval after S(1). We do this by dropping S(1) |
|
90 ## from the sort! E.g., S=[2 4 6], a=[1 2 3 4 5 6 7] |
|
91 ## unsorted [S(2:3) a] =[4 6 1 2 3 4 5 6 7 ] |
|
92 ## sorted [S(2:3) a] = [ 1 2 3 4 4 5 6 6 7 ] |
|
93 ## source index p = [ 3 4 5 1 6 7 2 8 9 ] |
|
94 ## boolean p<=l(S)-1 = [ 0 0 0 1 0 0 1 0 0 ] |
|
95 ## cumsum(p<=l(S)-1) = [ 0 0 0 1 1 1 2 2 2 ] |
|
96 ## Now we can use Octave's lvalue indexing to "invert" the sort, |
|
97 ## and assign all these indices back to the appropriate A and S, |
|
98 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to |
|
99 ## a_idx, and we know which interval S(i) contains a. It is |
|
100 ## easy to now check membership by comparing S(a_idx) == a. This |
|
101 ## magic works because S starts out sorted, and because sort |
|
102 ## preserves the relative order of identical elements. |
7068
|
103 [v, p] = sort ([S(2:lt); a(:)]); |
5178
|
104 idx(p) = cumsum (p <= lt-1) + 1; |
6240
|
105 idx = idx(lt:end); |
7068
|
106 if (iscell (a) || iscell (S)) |
5205
|
107 c = (cellfun ("length", a) |
7068
|
108 == reshape (cellfun ("length", S(idx)), size (a))); |
5205
|
109 idx2 = find (c); |
7068
|
110 c(idx2) = all (char (a(idx2)) == char (S(idx)(idx2)), 2); |
5205
|
111 else |
7068
|
112 c = (a == reshape (S (idx), size (a))); |
5205
|
113 endif |
5178
|
114 endif |
|
115 endif |
5181
|
116 |
5178
|
117 endfunction |
6533
|
118 |
|
119 %!assert (ismember ({''}, {'abc', 'def'}), false); |
|
120 %!assert (ismember ('abc', {'abc', 'def'}), true); |
|
121 %!assert (isempty (ismember ([], [1, 2])), true); |
6730
|
122 %!xtest assert (ismember ('', {'abc', 'def'}), false); |
7068
|
123 %!xtest fail ('ismember ([], {1, 2})', 'error:.*'); |
6664
|
124 %!fail ('ismember ({[]}, {1, 2})', 'error:.*'); |
6609
|
125 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0])) |
|
126 %!assert (ismember ({'foo'}, {'foobar'}), false) |
|
127 %!assert (ismember ({'bar'}, {'foobar'}), false) |
|
128 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true) |
|
129 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])) |
|
130 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])) |
7068
|
131 %!assert (ismember ("1", "0123456789."), true) |