annotate scripts/set/ismember.m @ 6046:34f96dd5441b

[project @ 2006-10-10 16:10:25 by jwe]
author jwe
date Tue, 10 Oct 2006 16:10:31 +0000
parents 2618a0750ae6
children 3e5a3b5fd40e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2000 Paul Kienzle
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
2 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
3 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
4 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
6 ## under the terms of the GNU General Public License as published by
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
8 ## any later version.
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
9 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
13 ## General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
14 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
5307
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5205
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5205
diff changeset
18 ## 02110-1301, USA.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
19
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
21 ## @deftypefn {Function File} {} ismember (@var{A}, @var{S})
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
22 ## Return a matrix the same shape as @var{A} which has 1 if
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
23 ## @code{A(i,j)} is in @var{S} or 0 if it isn't.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5435
diff changeset
24 ## @seealso{unique, union, intersection, setxor, setdiff}
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
25 ## @end deftypefn
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
26
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
27 ## Author: Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
28 ## Adapted-by: jwe
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
29
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
30 function c = ismember (a, S)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
31
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
32 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
33 print_usage ();
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
34 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
35
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
36 [ra, ca] = size (a);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
37 if (isempty (a) || isempty (S))
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
38 c = zeros (ra, ca);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
39 else
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
40 if (iscell (a) && ! iscell (S))
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
41 tmp{1} = S;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
42 S = tmp;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
43 endif
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
44 if (! iscell (a) && iscell (S))
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
45 tmp{1} = a;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
46 a = tmp;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
47 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
48 S = unique (S(:));
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
49 lt = length (S);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
50 if (lt == 1)
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
51 if (iscell (a) || iscell (S))
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
52 c = cellfun ("length", a) == cellfun ("length", S);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
53 idx = find (c);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
54 c(idx) = all (char (a(idx)) == repmat (char (S), length (idx), 1), 2);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
55 else
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
56 c = (a == S);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
57 endif
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
58 elseif (prod (size (a)) == 1)
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
59 if (iscell (a) || iscell (S))
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
60 c = cellfun ("length", a) == cellfun ("length", S);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
61 idx = find (c);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
62 c(idx) = all (repmat (char (a), length (idx), 1) == char (S(idx)), 2);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
63 c = any(c);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
64 else
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
65 c = any (a == S);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
66 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
67 else
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
68 ## Magic: the following code determines for each a, the index i
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
69 ## such that S(i)<= a < S(i+1). It does this by sorting the a
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
70 ## into S and remembering the source index where each element came
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
71 ## from. Since all the a's originally came after all the S's, if
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
72 ## the source index is less than the length of S, then the element
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
73 ## came from S. We can then do a cumulative sum on the indices to
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
74 ## figure out which element of S each a comes after.
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
75 ## E.g., S=[2 4 6], a=[1 2 3 4 5 6 7]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
76 ## unsorted [S a] = [ 2 4 6 1 2 3 4 5 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
77 ## sorted [ S a ] = [ 1 2 2 3 4 4 5 6 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
78 ## source index p = [ 4 1 5 6 2 7 8 3 9 10 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
79 ## boolean p<=l(S) = [ 0 1 0 0 1 0 0 1 0 0 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
80 ## cumsum(p<=l(S)) = [ 0 1 1 1 2 2 2 3 3 3 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
81 ## Note that this leaves a(1) coming after S(0) which doesn't
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
82 ## exist. So arbitrarily, we will dump all elements less than
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
83 ## S(1) into the interval after S(1). We do this by dropping S(1)
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
84 ## from the sort! E.g., S=[2 4 6], a=[1 2 3 4 5 6 7]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
85 ## unsorted [S(2:3) a] =[4 6 1 2 3 4 5 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
86 ## sorted [S(2:3) a] = [ 1 2 3 4 4 5 6 6 7 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
87 ## source index p = [ 3 4 5 1 6 7 2 8 9 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
88 ## boolean p<=l(S)-1 = [ 0 0 0 1 0 0 1 0 0 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
89 ## cumsum(p<=l(S)-1) = [ 0 0 0 1 1 1 2 2 2 ]
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
90 ## Now we can use Octave's lvalue indexing to "invert" the sort,
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
91 ## and assign all these indices back to the appropriate A and S,
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
92 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
93 ## a_idx, and we know which interval S(i) contains a. It is
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
94 ## easy to now check membership by comparing S(a_idx) == a. This
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
95 ## magic works because S starts out sorted, and because sort
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
96 ## preserves the relative order of identical elements.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
97 [v, p] = sort ([S(2:lt); a(:)]);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
98 idx(p) = cumsum (p <= lt-1) + 1;
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
99 idx = idx(lt:lt+ra*ca-1);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
100 if (iscell (a) || iscell (S))
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
101 c = (cellfun ("length", a)
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
102 == reshape (cellfun ("length", S(idx)), size (a)));
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
103 idx2 = find (c);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
104 c(idx2) = all (char (a(idx2)) == char (S(idx)(idx2)), 2);
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
105 else
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
106 c = (a == reshape (S (idx), size (a)));
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents: 5182
diff changeset
107 endif
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
108 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
109 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
110
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
111 endfunction
5435
66ad03c58880 [project @ 2005-08-31 20:41:47 by jwe]
jwe
parents: 5307
diff changeset
112