]> Creatis software - CreaPhase.git/blob - octave_packages/m/set/ismember.m
update packages
[CreaPhase.git] / octave_packages / m / set / ismember.m
1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ## Copyright (C) 2009 Jaroslav Hajek
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {@var{tf} =} ismember (@var{A}, @var{s})
22 ## @deftypefnx {Function File} {[@var{tf}, @var{S_idx}] =} ismember (@var{A}, @var{s})
23 ## @deftypefnx {Function File} {[@var{tf}, @var{S_idx}] =} ismember (@var{A}, @var{s}, "rows")
24 ## Return a logical matrix @var{tf} with the same shape as @var{A} which is
25 ## true (1) if @code{A(i,j)} is in @var{s} and false (0) if it is not.  If a
26 ## second output argument is requested, the index into @var{s} of each of the
27 ## matching elements is also returned.
28 ##
29 ## @example
30 ## @group
31 ## a = [3, 10, 1];
32 ## s = [0:9];
33 ## [tf, s_idx] = ismember (a, s)
34 ##      @result{} tf = [1, 0, 1]
35 ##      @result{} s_idx = [4, 0, 2]
36 ## @end group
37 ## @end example
38 ##
39 ## The inputs, @var{A} and @var{s}, may also be cell arrays.
40 ##
41 ## @example
42 ## @group
43 ## a = @{'abc'@};
44 ## s = @{'abc', 'def'@};
45 ## [tf, s_idx] = ismember (a, s)
46 ##      @result{} tf = [1, 0]
47 ##      @result{} s_idx = [1, 0]
48 ## @end group
49 ## @end example
50 ##
51 ## With the optional third argument @code{"rows"}, and matrices
52 ## @var{A} and @var{s} with the same number of columns, compare rows in
53 ## @var{A} with the rows in @var{s}.
54 ##
55 ## @example
56 ## @group
57 ## a = [1:3; 5:7; 4:6];
58 ## s = [0:2; 1:3; 2:4; 3:5; 4:6];
59 ## [tf, s_idx] = ismember(a, s, "rows")
60 ##      @result{} tf = logical ([1; 0; 1])
61 ##      @result{} s_idx = [2; 0; 5];
62 ## @end group
63 ## @end example
64 ##
65 ## @seealso{unique, union, intersect, setxor, setdiff}
66 ## @end deftypefn
67
68 ## Author: Paul Kienzle <pkienzle@users.sf.net>
69 ## Author: Søren Hauberg <hauberg@gmail.com>
70 ## Author: Ben Abbott <bpabbott@mac.com>
71 ## Adapted-by: jwe
72 ## Reimplemented using lookup & unique: Jaroslav Hajek <highegg@gmail.com>
73
74 function [tf, a_idx] = ismember (A, s, varargin)
75
76   if (nargin < 2 || nargin > 3)
77     print_usage ();
78   endif
79
80   ## lookup() does not handle logical values
81   if (islogical (A))
82     A = uint8 (A);
83   endif
84   if (islogical (s))
85     s = uint8 (s);
86   endif
87
88   [A, s] = validargs ("ismember", A, s, varargin{:});
89
90   if (nargin == 2)
91     s = s(:);
92     ## We do it this way, because we expect the array to be often sorted.
93     if (issorted (s))
94       is = [];
95     else
96       [s, is] = sort (s);
97     endif
98
99     ## sort out NaNs in table
100     if (isreal (s) && ! isempty (s) && isnan (s(end)))
101         s = s(1:end - sum (isnan (s)));
102     endif
103
104     if (nargout > 1)
105       a_idx = lookup (s, A, "m");
106       tf = logical (a_idx);
107       if (! isempty (is))
108         a_idx(tf) = is (a_idx(tf));
109       endif
110     else
111       tf = lookup (s, A, "b");
112     endif
113
114   else
115
116     if (isempty (A) || isempty (s))
117       tf = false (rows (A), 1);
118       a_idx = zeros (rows (A), 1);
119     else
120
121       ## FIXME: lookup does not support "rows", so we just use unique.
122       [xx, ii, jj] = unique ([A; s], "rows", "last");
123       na = rows (A);
124       jj = ii(jj(1:na));
125       tf = jj > na;
126
127       if (nargout > 1)
128         a_idx = max (0, jj - na);
129       endif
130
131     endif
132   endif
133
134 endfunction
135
136 %!assert (ismember ({''}, {'abc', 'def'}), false);
137 %!assert (ismember ('abc', {'abc', 'def'}), true);
138 %!assert (isempty (ismember ([], [1, 2])), true);
139 %!assert (isempty (ismember ({}, {'a', 'b'})), true);
140 %!assert (ismember ('', {'abc', 'def'}), false);
141 %!fail ('ismember ([], {1, 2})');
142 %!fail ('ismember ({[]}, {1, 2})');
143 %!fail ('ismember ({}, {1, 2})');
144 %!fail ('ismember ({1}, {''1'', ''2''})');
145 %!fail ('ismember (1, ''abc'')');
146 %!fail ('ismember ({''1''}, {''1'', ''2''},''rows'')');
147 %!fail ('ismember ([1 2 3], [5 4 3 1], ''rows'')');
148 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0]));
149 %!assert (ismember ({'foo'}, {'foobar'}), false);
150 %!assert (ismember ({'bar'}, {'foobar'}), false);
151 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true);
152 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1]));
153 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1]));
154 %!assert (ismember ("1", "0123456789."), true);
155
156 %!test
157 %! [result, a_idx] = ismember ([1, 2], []);
158 %! assert (result, logical ([0, 0]))
159 %! assert (a_idx, [0, 0]);
160
161 %!test
162 %! [result, a_idx] = ismember ([], [1, 2]);
163 %! assert (result, logical ([]))
164 %! assert (a_idx, []);
165
166 %!test
167 %! [result, a_idx] = ismember ({'a', 'b'}, '');
168 %! assert (result, logical ([0, 0]))
169 %! assert (a_idx, [0, 0]);
170
171 %!test
172 %! [result, a_idx] = ismember ({'a', 'b'}, {});
173 %! assert (result, logical ([0, 0]))
174 %! assert (a_idx, [0, 0]);
175
176 %!test
177 %! [result, a_idx] = ismember ('', {'a', 'b'});
178 %! assert (result, false)
179 %! assert (a_idx, 0);
180
181 %!test
182 %! [result, a_idx] = ismember ({}, {'a', 'b'});
183 %! assert (result, logical ([]))
184 %! assert (a_idx, []);
185
186 %!test
187 %! [result, a_idx] = ismember([1 2 3 4 5], [3]);
188 %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0]));
189
190 %!test
191 %! [result, a_idx] = ismember([1 6], [1 2 3 4 5 1 6 1]);
192 %! assert (all (result == logical ([1 1])) && a_idx(2) == 7);
193
194 %!test
195 %! [result, a_idx] = ismember ([3,10,1], [0,1,2,3,4,5,6,7,8,9]);
196 %! assert (all (result == logical ([1, 0, 1])) && all (a_idx == [4, 0, 2]));
197
198 %!test
199 %! [result, a_idx] = ismember ("1.1", "0123456789.1");
200 %! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [12, 11, 12]));
201
202 %!test
203 %! [result, a_idx] = ismember([1:3; 5:7; 4:6], [0:2; 1:3; 2:4; 3:5; 4:6], 'rows');
204 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [2; 0; 5]));
205
206 %!test
207 %! [result, a_idx] = ismember([1.1,1.2,1.3; 2.1,2.2,2.3; 10,11,12], [1.1,1.2,1.3; 10,11,12; 2.12,2.22,2.32], 'rows');
208 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [1; 0; 2]));
209