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