comparison scripts/set/ismember.m @ 5205:59592dcbb5d8

[project @ 2005-03-15 16:04:56 by jwe]
author jwe
date Tue, 15 Mar 2005 16:04:57 +0000
parents 5b361aa47dff
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
5204:2a44a667da02 5205:59592dcbb5d8
35 35
36 [ra, ca] = size (a); 36 [ra, ca] = size (a);
37 if (isempty (a) || isempty (S)) 37 if (isempty (a) || isempty (S))
38 c = zeros (ra, ca); 38 c = zeros (ra, ca);
39 else 39 else
40 if (iscell (a) && ! iscell (S))
41 tmp{1} = S;
42 S = tmp;
43 endif
44 if (! iscell (a) && iscell (S))
45 tmp{1} = a;
46 a = tmp;
47 endif
40 S = unique (S(:)); 48 S = unique (S(:));
41 lt = length (S); 49 lt = length (S);
42 if (lt == 1) 50 if (lt == 1)
43 c = (a == S); 51 if (iscell (a) || iscell (S))
44 elseif (ra*ca == 1) 52 c = cellfun ("length", a) == cellfun ("length", S);
45 c = any (a == S); 53 idx = find (c);
54 c(idx) = all (char (a(idx)) == repmat (char (S), length (idx), 1), 2);
55 else
56 c = (a == S);
57 endif
58 elseif (prod (size (a)) == 1)
59 if (iscell (a) || iscell (S))
60 c = cellfun ("length", a) == cellfun ("length", S);
61 idx = find (c);
62 c(idx) = all (repmat (char (a), length (idx), 1) == char (S(idx)), 2);
63 c = any(c);
64 else
65 c = any (a == S);
66 endif
46 else 67 else
47 ## Magic: the following code determines for each a, the index i 68 ## Magic: the following code determines for each a, the index i
48 ## such that S(i)<= a < S(i+1). It does this by sorting the a 69 ## such that S(i)<= a < S(i+1). It does this by sorting the a
49 ## into S and remembering the source index where each element came 70 ## into S and remembering the source index where each element came
50 ## from. Since all the a's originally came after all the S's, if 71 ## from. Since all the a's originally came after all the S's, if
74 ## magic works because S starts out sorted, and because sort 95 ## magic works because S starts out sorted, and because sort
75 ## preserves the relative order of identical elements. 96 ## preserves the relative order of identical elements.
76 [v, p] = sort ([S(2:lt); a(:)]); 97 [v, p] = sort ([S(2:lt); a(:)]);
77 idx(p) = cumsum (p <= lt-1) + 1; 98 idx(p) = cumsum (p <= lt-1) + 1;
78 idx = idx(lt:lt+ra*ca-1); 99 idx = idx(lt:lt+ra*ca-1);
79 c = (a == reshape (S(idx), size (a))); 100 if (iscell (a) || iscell (S))
101 c = (cellfun ("length", a)
102 == reshape (cellfun ("length", S(idx)), size (a)));
103 idx2 = find (c);
104 c(idx2) = all (char (a(idx2)) == char (S(idx)(idx2)), 2);
105 else
106 c = (a == reshape (S (idx), size (a)));
107 endif
80 endif 108 endif
81 endif 109 endif
82 110
83 endfunction 111 endfunction
84 112