5178
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
5181
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
5178
|
19 |
|
20 ## -*- texinfo -*- |
5182
|
21 ## @deftypefn {Function File} {} ismember (@var{A}, @var{S}) |
5178
|
22 ## Return a matrix 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. |
5642
|
24 ## @seealso{unique, union, intersection, setxor, setdiff} |
5178
|
25 ## @end deftypefn |
|
26 |
5181
|
27 ## Author: Paul Kienzle |
|
28 ## Adapted-by: jwe |
|
29 |
|
30 function c = ismember (a, S) |
|
31 |
|
32 if (nargin != 2) |
6046
|
33 print_usage (); |
5178
|
34 endif |
|
35 |
5181
|
36 if (isempty (a) || isempty (S)) |
6240
|
37 c = zeros (size (a)); |
5178
|
38 else |
5205
|
39 if (iscell (a) && ! iscell (S)) |
|
40 tmp{1} = S; |
|
41 S = tmp; |
|
42 endif |
|
43 if (! iscell (a) && iscell (S)) |
|
44 tmp{1} = a; |
|
45 a = tmp; |
|
46 endif |
5181
|
47 S = unique (S(:)); |
|
48 lt = length (S); |
|
49 if (lt == 1) |
5205
|
50 if (iscell (a) || iscell (S)) |
|
51 c = cellfun ("length", a) == cellfun ("length", S); |
|
52 idx = find (c); |
|
53 c(idx) = all (char (a(idx)) == repmat (char (S), length (idx), 1), 2); |
|
54 else |
|
55 c = (a == S); |
|
56 endif |
|
57 elseif (prod (size (a)) == 1) |
|
58 if (iscell (a) || iscell (S)) |
|
59 c = cellfun ("length", a) == cellfun ("length", S); |
|
60 idx = find (c); |
|
61 c(idx) = all (repmat (char (a), length (idx), 1) == char (S(idx)), 2); |
|
62 c = any(c); |
|
63 else |
|
64 c = any (a == S); |
|
65 endif |
5178
|
66 else |
5181
|
67 ## Magic: the following code determines for each a, the index i |
5178
|
68 ## such that S(i)<= a < S(i+1). It does this by sorting the a |
|
69 ## into S and remembering the source index where each element came |
|
70 ## from. Since all the a's originally came after all the S's, if |
|
71 ## the source index is less than the length of S, then the element |
|
72 ## came from S. We can then do a cumulative sum on the indices to |
|
73 ## figure out which element of S each a comes after. |
|
74 ## E.g., S=[2 4 6], a=[1 2 3 4 5 6 7] |
|
75 ## unsorted [S a] = [ 2 4 6 1 2 3 4 5 6 7 ] |
|
76 ## sorted [ S a ] = [ 1 2 2 3 4 4 5 6 6 7 ] |
|
77 ## source index p = [ 4 1 5 6 2 7 8 3 9 10 ] |
|
78 ## boolean p<=l(S) = [ 0 1 0 0 1 0 0 1 0 0 ] |
|
79 ## cumsum(p<=l(S)) = [ 0 1 1 1 2 2 2 3 3 3 ] |
|
80 ## Note that this leaves a(1) coming after S(0) which doesn't |
|
81 ## exist. So arbitrarily, we will dump all elements less than |
|
82 ## S(1) into the interval after S(1). We do this by dropping S(1) |
|
83 ## from the sort! E.g., S=[2 4 6], a=[1 2 3 4 5 6 7] |
|
84 ## unsorted [S(2:3) a] =[4 6 1 2 3 4 5 6 7 ] |
|
85 ## sorted [S(2:3) a] = [ 1 2 3 4 4 5 6 6 7 ] |
|
86 ## source index p = [ 3 4 5 1 6 7 2 8 9 ] |
|
87 ## boolean p<=l(S)-1 = [ 0 0 0 1 0 0 1 0 0 ] |
|
88 ## cumsum(p<=l(S)-1) = [ 0 0 0 1 1 1 2 2 2 ] |
|
89 ## Now we can use Octave's lvalue indexing to "invert" the sort, |
|
90 ## and assign all these indices back to the appropriate A and S, |
|
91 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to |
|
92 ## a_idx, and we know which interval S(i) contains a. It is |
|
93 ## easy to now check membership by comparing S(a_idx) == a. This |
|
94 ## magic works because S starts out sorted, and because sort |
|
95 ## preserves the relative order of identical elements. |
5181
|
96 [v, p] = sort ([S(2:lt); a(:)]); |
5178
|
97 idx(p) = cumsum (p <= lt-1) + 1; |
6240
|
98 idx = idx(lt:end); |
5205
|
99 if (iscell (a) || iscell (S)) |
|
100 c = (cellfun ("length", a) |
|
101 == reshape (cellfun ("length", S(idx)), size (a))); |
|
102 idx2 = find (c); |
|
103 c(idx2) = all (char (a(idx2)) == char (S(idx)(idx2)), 2); |
|
104 else |
|
105 c = (a == reshape (S (idx), size (a))); |
|
106 endif |
5178
|
107 endif |
|
108 endif |
5181
|
109 |
5178
|
110 endfunction |
5435
|
111 |