1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ## Copyright (C) 2009 Jaroslav Hajek
4 ## This file is part of Octave.
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.
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.
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/>.
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.
33 ## [tf, s_idx] = ismember (a, s)
34 ## @result{} tf = [1, 0, 1]
35 ## @result{} s_idx = [4, 0, 2]
39 ## The inputs, @var{A} and @var{s}, may also be cell arrays.
44 ## s = @{'abc', 'def'@};
45 ## [tf, s_idx] = ismember (a, s)
46 ## @result{} tf = [1, 0]
47 ## @result{} s_idx = [1, 0]
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}.
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];
65 ## @seealso{unique, union, intersect, setxor, setdiff}
68 ## Author: Paul Kienzle <pkienzle@users.sf.net>
69 ## Author: Søren Hauberg <hauberg@gmail.com>
70 ## Author: Ben Abbott <bpabbott@mac.com>
72 ## Reimplemented using lookup & unique: Jaroslav Hajek <highegg@gmail.com>
74 function [tf, a_idx] = ismember (A, s, varargin)
76 if (nargin < 2 || nargin > 3)
80 ## lookup() does not handle logical values
88 [A, s] = validargs ("ismember", A, s, varargin{:});
92 ## We do it this way, because we expect the array to be often sorted.
99 ## sort out NaNs in table
100 if (isreal (s) && ! isempty (s) && isnan (s(end)))
101 s = s(1:end - sum (isnan (s)));
105 a_idx = lookup (s, A, "m");
106 tf = logical (a_idx);
108 a_idx(tf) = is (a_idx(tf));
111 tf = lookup (s, A, "b");
116 if (isempty (A) || isempty (s))
117 tf = false (rows (A), 1);
118 a_idx = zeros (rows (A), 1);
121 ## FIXME: lookup does not support "rows", so we just use unique.
122 [xx, ii, jj] = unique ([A; s], "rows", "last");
128 a_idx = max (0, jj - na);
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);
157 %! [result, a_idx] = ismember ([1, 2], []);
158 %! assert (result, logical ([0, 0]))
159 %! assert (a_idx, [0, 0]);
162 %! [result, a_idx] = ismember ([], [1, 2]);
163 %! assert (result, logical ([]))
164 %! assert (a_idx, []);
167 %! [result, a_idx] = ismember ({'a', 'b'}, '');
168 %! assert (result, logical ([0, 0]))
169 %! assert (a_idx, [0, 0]);
172 %! [result, a_idx] = ismember ({'a', 'b'}, {});
173 %! assert (result, logical ([0, 0]))
174 %! assert (a_idx, [0, 0]);
177 %! [result, a_idx] = ismember ('', {'a', 'b'});
178 %! assert (result, false)
179 %! assert (a_idx, 0);
182 %! [result, a_idx] = ismember ({}, {'a', 'b'});
183 %! assert (result, logical ([]))
184 %! assert (a_idx, []);
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]));
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);
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]));
199 %! [result, a_idx] = ismember ("1.1", "0123456789.1");
200 %! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [12, 11, 12]));
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]));
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]));