comparison scripts/set/ismember.m @ 5181:41cd70503c72

[project @ 2005-03-03 05:49:55 by jwe]
author jwe
date Thu, 03 Mar 2005 05:49:55 +0000
parents 6758c11b5b99
children 5b361aa47dff
comparison
equal deleted inserted replaced
5180:e7438487c857 5181:41cd70503c72
1 ## Copyright (C) 2000 Paul Kienzle 1 ## Copyright (C) 2000 Paul Kienzle
2 ## 2 ##
3 ## This program is free software; you can redistribute it and/or modify 3 ## This file is part of Octave.
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 4 ##
8 ## This program is distributed in the hope that it will be useful, 5 ## Octave is free software; you can redistribute it and/or modify it
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 6 ## under the terms of the GNU General Public License as published by
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 7 ## the Free Software Foundation; either version 2, or (at your option)
11 ## GNU General Public License for more details. 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.
12 ## 14 ##
13 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software 16 ## along with Octave; see the file COPYING. If not, write to the Free
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 ## 02111-1307, USA.
16 19
17 ## -*- texinfo -*- 20 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {} ismember(@var{A}, @var{S}) 21 ## @deftypefn {Function File} {} ismember(@var{A}, @var{S})
19 ## 22 ##
20 ## Return a matrix the same shape as @var{A} which has 1 if 23 ## Return a matrix the same shape as @var{A} which has 1 if
21 ## @code{A(i,j)} is in @var{S} or 0 if it isn't. 24 ## @code{A(i,j)} is in @var{S} or 0 if it isn't.
22 ## 25 ##
23 ## @end deftypefn 26 ## @end deftypefn
24 ## @seealso{unique, union, intersect, setxor, setdiff} 27 ## @seealso{unique, union, intersect, setxor, setdiff}
25 28
26 function c = ismember(a,S) 29 ## Author: Paul Kienzle
27 if nargin != 2 30 ## Adapted-by: jwe
28 usage("ismember(A,S)"); 31
32 function c = ismember (a, S)
33
34 if (nargin != 2)
35 usage ("ismember (A, S)");
29 endif 36 endif
30 37
31 [ra, ca] = size(a); 38 [ra, ca] = size (a);
32 if isempty(a) || isempty(S) 39 if (isempty (a) || isempty (S))
33 c = zeros(ra, ca); 40 c = zeros (ra, ca);
34 else 41 else
35 S = unique(S(:)); 42 S = unique (S(:));
36 lt = length(S); 43 lt = length (S);
37 if lt == 1 44 if (lt == 1)
38 c = ( a == S ); 45 c = (a == S);
39 elseif ra*ca == 1 46 elseif (ra*ca == 1)
40 c = any (a == S); 47 c = any (a == S);
41 else 48 else
42 ## Magic : the following code determines for each a, the index i 49 ## Magic: the following code determines for each a, the index i
43 ## such that S(i)<= a < S(i+1). It does this by sorting the a 50 ## such that S(i)<= a < S(i+1). It does this by sorting the a
44 ## into S and remembering the source index where each element came 51 ## into S and remembering the source index where each element came
45 ## from. Since all the a's originally came after all the S's, if 52 ## from. Since all the a's originally came after all the S's, if
46 ## the source index is less than the length of S, then the element 53 ## the source index is less than the length of S, then the element
47 ## came from S. We can then do a cumulative sum on the indices to 54 ## came from S. We can then do a cumulative sum on the indices to
66 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to 73 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to
67 ## a_idx, and we know which interval S(i) contains a. It is 74 ## a_idx, and we know which interval S(i) contains a. It is
68 ## easy to now check membership by comparing S(a_idx) == a. This 75 ## easy to now check membership by comparing S(a_idx) == a. This
69 ## magic works because S starts out sorted, and because sort 76 ## magic works because S starts out sorted, and because sort
70 ## preserves the relative order of identical elements. 77 ## preserves the relative order of identical elements.
71 [v, p] = sort ( [ S(2:lt) ; a(:) ] ); 78 [v, p] = sort ([S(2:lt); a(:)]);
72 idx(p) = cumsum (p <= lt-1) + 1; 79 idx(p) = cumsum (p <= lt-1) + 1;
73 idx = idx (lt : lt+ra*ca-1); 80 idx = idx(lt:lt+ra*ca-1);
74 c = ( a == reshape (S (idx), size (a)) ); 81 c = (a == reshape (S(idx), size (a)));
75 endif 82 endif
76 endif 83 endif
84
77 endfunction 85 endfunction
78 86