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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, 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. |
|
24 ## @end deftypefn |
|
25 ## @seealso{unique, union, intersect, setxor, setdiff} |
|
26 |
5181
|
27 ## Author: Paul Kienzle |
|
28 ## Adapted-by: jwe |
|
29 |
|
30 function c = ismember (a, S) |
|
31 |
|
32 if (nargin != 2) |
|
33 usage ("ismember (A, S)"); |
5178
|
34 endif |
|
35 |
5181
|
36 [ra, ca] = size (a); |
|
37 if (isempty (a) || isempty (S)) |
|
38 c = zeros (ra, ca); |
5178
|
39 else |
5181
|
40 S = unique (S(:)); |
|
41 lt = length (S); |
|
42 if (lt == 1) |
|
43 c = (a == S); |
|
44 elseif (ra*ca == 1) |
5178
|
45 c = any (a == S); |
|
46 else |
5181
|
47 ## Magic: the following code determines for each a, the index i |
5178
|
48 ## 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 |
|
50 ## from. Since all the a's originally came after all the S's, if |
|
51 ## the source index is less than the length of S, then the element |
|
52 ## came from S. We can then do a cumulative sum on the indices to |
|
53 ## figure out which element of S each a comes after. |
|
54 ## E.g., S=[2 4 6], a=[1 2 3 4 5 6 7] |
|
55 ## unsorted [S a] = [ 2 4 6 1 2 3 4 5 6 7 ] |
|
56 ## sorted [ S a ] = [ 1 2 2 3 4 4 5 6 6 7 ] |
|
57 ## source index p = [ 4 1 5 6 2 7 8 3 9 10 ] |
|
58 ## boolean p<=l(S) = [ 0 1 0 0 1 0 0 1 0 0 ] |
|
59 ## cumsum(p<=l(S)) = [ 0 1 1 1 2 2 2 3 3 3 ] |
|
60 ## Note that this leaves a(1) coming after S(0) which doesn't |
|
61 ## exist. So arbitrarily, we will dump all elements less than |
|
62 ## S(1) into the interval after S(1). We do this by dropping S(1) |
|
63 ## from the sort! E.g., S=[2 4 6], a=[1 2 3 4 5 6 7] |
|
64 ## unsorted [S(2:3) a] =[4 6 1 2 3 4 5 6 7 ] |
|
65 ## sorted [S(2:3) a] = [ 1 2 3 4 4 5 6 6 7 ] |
|
66 ## source index p = [ 3 4 5 1 6 7 2 8 9 ] |
|
67 ## boolean p<=l(S)-1 = [ 0 0 0 1 0 0 1 0 0 ] |
|
68 ## cumsum(p<=l(S)-1) = [ 0 0 0 1 1 1 2 2 2 ] |
|
69 ## Now we can use Octave's lvalue indexing to "invert" the sort, |
|
70 ## and assign all these indices back to the appropriate A and S, |
|
71 ## giving S_idx = [ -- 1 2], a_idx = [ 0 0 0 1 1 2 2 ]. Add 1 to |
|
72 ## a_idx, and we know which interval S(i) contains a. It is |
|
73 ## easy to now check membership by comparing S(a_idx) == a. This |
|
74 ## magic works because S starts out sorted, and because sort |
|
75 ## preserves the relative order of identical elements. |
5181
|
76 [v, p] = sort ([S(2:lt); a(:)]); |
5178
|
77 idx(p) = cumsum (p <= lt-1) + 1; |
5181
|
78 idx = idx(lt:lt+ra*ca-1); |
|
79 c = (a == reshape (S(idx), size (a))); |
5178
|
80 endif |
|
81 endif |
5181
|
82 |
5178
|
83 endfunction |
|
84 |