annotate 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
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
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
18 ## 02111-1307, 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 -*-
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {} ismember(@var{A}, @var{S})
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
22 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
23 ## 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
24 ## @code{A(i,j)} is in @var{S} or 0 if it isn't.
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
25 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
27 ## @seealso{unique, union, intersect, setxor, setdiff}
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
28
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
29 ## Author: Paul Kienzle
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
30 ## Adapted-by: jwe
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 function c = ismember (a, S)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
33
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
34 if (nargin != 2)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
35 usage ("ismember (A, S)");
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
36 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
37
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
38 [ra, ca] = size (a);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
39 if (isempty (a) || isempty (S))
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
40 c = zeros (ra, ca);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
41 else
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
42 S = unique (S(:));
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
43 lt = length (S);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
44 if (lt == 1)
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
45 c = (a == S);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
46 elseif (ra*ca == 1)
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
47 c = any (a == S);
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
48 else
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
49 ## 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
50 ## 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
51 ## 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
52 ## 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
53 ## 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
54 ## 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
55 ## figure out which element of S each a comes after.
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
56 ## 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
57 ## 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
58 ## 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
59 ## 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
60 ## 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
61 ## 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
62 ## 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
63 ## exist. So arbitrarily, we will dump all elements less than
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
64 ## 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
65 ## 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
66 ## 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
67 ## 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
68 ## 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
69 ## 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
70 ## 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
71 ## 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
72 ## 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
73 ## 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
74 ## 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
75 ## 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
76 ## magic works because S starts out sorted, and because sort
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
77 ## preserves the relative order of identical elements.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
78 [v, p] = sort ([S(2:lt); a(:)]);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
79 idx(p) = cumsum (p <= lt-1) + 1;
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
80 idx = idx(lt:lt+ra*ca-1);
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
81 c = (a == reshape (S(idx), size (a)));
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
82 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
83 endif
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
84
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
85 endfunction
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
86