Mercurial > hg > octave-nkf
annotate scripts/set/ismember.m @ 9345:dbc61d4e428d
optimize ismember
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 14 Jun 2009 11:57:52 +0200 |
parents | f5e4b5fd1f1e |
children | 6c255e51ef7e |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2006, 2007, 2008, 2009 Paul Kienzle |
9345 | 2 ## Copyright (C) 2009 Jaroslav Hajek |
5178 | 3 ## |
5181 | 4 ## This file is part of Octave. |
5178 | 5 ## |
5181 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5181 | 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. | |
5178 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5178 | 19 |
20 ## -*- texinfo -*- | |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
21 ## @deftypefn {Function File} {[@var{tf} =} ismember (@var{A}, @var{S}) |
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{tf}, @var{S_idx}] =} ismember (@var{A}, @var{S}) |
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{tf}, @var{S_idx}] =} ismember (@var{A}, @var{S}, "rows") |
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
24 ## Return a matrix @var{tf} with the same shape as @var{A} which has a 1 if |
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
25 ## @code{A(i,j)} is in @var{S} and 0 if it is not. If a second output argument |
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
26 ## is requested, the index into @var{S} of each of the matching elements is |
7128 | 27 ## also returned. |
28 ## | |
29 ## @example | |
30 ## @group | |
31 ## a = [3, 10, 1]; | |
32 ## s = [0:9]; | |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
33 ## [tf, s_idx] = ismember (a, s); |
7128 | 34 ## @result{} tf = [1, 0, 1] |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
35 ## @result{} s_idx = [4, 0, 2] |
7128 | 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'@}; | |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
45 ## [tf, s_idx] = ismember (a, s); |
7128 | 46 ## @result{} tf = [1, 0] |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
47 ## @result{} s_idx = [1, 0] |
7128 | 48 ## @end group |
49 ## @end example | |
50 ## | |
7129 | 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}. | |
7128 | 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]; | |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
59 ## [tf, s_idx] = ismember(a, s, 'rows'); |
7128 | 60 ## @result{} tf = logical ([1; 0; 1]) |
9276
f5e4b5fd1f1e
Update ismember examples to remove incorrect reference to residue function
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
61 ## @result{} s_idx = [2; 0; 5]; |
7128 | 62 ## @end group |
63 ## @end example | |
64 ## | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7883
diff
changeset
|
65 ## @seealso{unique, union, intersect, setxor, setdiff} |
5178 | 66 ## @end deftypefn |
67 | |
7129 | 68 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
69 ## Author: Søren Hauberg <hauberg@gmail.com> | |
70 ## Author: Ben Abbott <bpabbott@mac.com> | |
5181 | 71 ## Adapted-by: jwe |
9345 | 72 ## Reimplemented using lookup & unique: Jaroslav Hajek <highegg@gmail.com> |
5181 | 73 |
7128 | 74 function [tf, a_idx] = ismember (a, s, rows_opt) |
5181 | 75 |
9345 | 76 if (nargin == 2) |
77 ica = iscellstr (a); | |
78 ics = iscellstr (s); | |
79 if (ica || ics) | |
80 if (ica && ischar (s)) | |
81 s = cellstr (s); | |
82 elseif (ics && ischar (a)) | |
83 a = cellstr (a); | |
84 elseif (! (ica && ics)) | |
85 error ("ismember: invalid argument types"); | |
86 endif | |
87 elseif (! isa (a, class (s))) | |
88 error ("ismember: both input arguments must be the same type"); | |
89 elseif (! ischar (a) && ! isnumeric (a)) | |
90 error ("ismember: input arguments must be arrays, cell arrays, or strings"); | |
91 endif | |
92 | |
93 s = s(:); | |
94 ## We do it this way, because we expect the array to be often sorted. | |
95 if (issorted (s)) | |
96 is = []; | |
97 else | |
98 [s, is] = sort (s); | |
99 endif | |
100 | |
101 if (nargout > 1) | |
102 a_idx = lookup (s, a, "m"); | |
103 tf = logical (a_idx); | |
104 if (! isempty (is)) | |
105 a_idx(tf) = is (a_idx(tf)); | |
7128 | 106 endif |
107 else | |
9345 | 108 tf = lookup (s, a, "b"); |
109 endif | |
110 | |
111 elseif (nargin == 3 && strcmpi (rows_opt, "rows")) | |
112 if (iscell (a) || iscell (s)) | |
113 error ("ismember: cells not supported with ""rows"""); | |
114 elseif (! isa (a, class (s))) | |
115 error ("ismember: both input arguments must be the same type"); | |
116 elseif (! ischar (a) && ! isnumeric (a)) | |
117 error ("ismember: input arguments must be arrays, cell arrays, or strings"); | |
7128 | 118 endif |
9345 | 119 if (columns (a) != columns (s)) |
120 error ("ismember: number of columns must match"); | |
121 endif | |
122 | |
123 ## FIXME: lookup does not support "rows", so we just use unique. | |
124 [xx, ii, jj] = unique ([a; s], "rows", "last"); | |
125 na = rows (a); | |
126 jj = ii(jj(1:na)); | |
127 tf = jj > na; | |
128 | |
129 if (nargout > 1) | |
130 a_idx = max (0, jj - na); | |
131 endif | |
132 | |
7128 | 133 else |
6046 | 134 print_usage (); |
5178 | 135 endif |
136 | |
7128 | 137 endfunction |
138 | |
6533 | 139 %!assert (ismember ({''}, {'abc', 'def'}), false); |
140 %!assert (ismember ('abc', {'abc', 'def'}), true); | |
141 %!assert (isempty (ismember ([], [1, 2])), true); | |
7077 | 142 %!assert (isempty (ismember ({}, {'a', 'b'})), true); |
7128 | 143 %!assert (ismember ('', {'abc', 'def'}), false); |
7883
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
144 %!fail ('ismember ([], {1, 2})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
145 %!fail ('ismember ({[]}, {1, 2})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
146 %!fail ('ismember ({}, {1, 2})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
147 %!fail ('ismember ({1}, {''1'', ''2''})'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
148 %!fail ('ismember (1, ''abc'')'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
149 %!fail ('ismember ({''1''}, {''1'', ''2''},''rows'')'); |
3092dd54ad95
fix expected output from lasterr in tests; fix fail tests in ismember.m
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
150 %!fail ('ismember ([1 2 3], [5 4 3 1], ''rows'')'); |
7128 | 151 %!assert (ismember ({'foo', 'bar'}, {'foobar'}), logical ([0, 0])); |
152 %!assert (ismember ({'foo'}, {'foobar'}), false); | |
153 %!assert (ismember ({'bar'}, {'foobar'}), false); | |
154 %!assert (ismember ({'bar'}, {'foobar', 'bar'}), true); | |
155 %!assert (ismember ({'foo', 'bar'}, {'foobar', 'bar'}), logical ([0, 1])); | |
156 %!assert (ismember ({'xfb', 'f', 'b'}, {'fb', 'b'}), logical ([0, 0, 1])); | |
157 %!assert (ismember ("1", "0123456789."), true); | |
158 | |
159 %!test | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
160 %! [result, a_idx] = ismember ([1, 2], []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
161 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
162 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
163 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
164 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
165 %! [result, a_idx] = ismember ([], [1, 2]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
166 %! assert (result, logical ([])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
167 %! assert (a_idx, []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
168 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
169 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
170 %! [result, a_idx] = ismember ({'a', 'b'}, ''); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
171 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
172 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
173 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
174 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
175 %! [result, a_idx] = ismember ({'a', 'b'}, {}); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
176 %! assert (result, logical ([0, 0])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
177 %! assert (a_idx, [0, 0]); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
178 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
179 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
180 %! [result, a_idx] = ismember ('', {'a', 'b'}); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
181 %! assert (result, false) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
182 %! assert (a_idx, 0); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
183 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
184 %!test |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
185 %! [result, a_idx] = ismember ({}, {'a', 'b'}); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
186 %! assert (result, logical ([])) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
187 %! assert (a_idx, []); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
188 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7129
diff
changeset
|
189 %!test |
7128 | 190 %! [result, a_idx] = ismember([1 2 3 4 5], [3]); |
191 %! assert (all (result == logical ([0 0 1 0 0])) && all (a_idx == [0 0 1 0 0])); | |
192 | |
193 %!test | |
194 %! [result, a_idx] = ismember([1 6], [1 2 3 4 5 1 6 1]); | |
9345 | 195 %! assert (all (result == logical ([1 1])) && a_idx(2) == 7); |
7128 | 196 |
197 %!test | |
198 %! [result, a_idx] = ismember ([3,10,1], [0,1,2,3,4,5,6,7,8,9]); | |
199 %! assert (all (result == logical ([1, 0, 1])) && all (a_idx == [4, 0, 2])); | |
200 | |
201 %!test | |
202 %! [result, a_idx] = ismember ("1.1", "0123456789.1"); | |
9345 | 203 %! assert (all (result == logical ([1, 1, 1])) && all (a_idx == [2, 11, 2])); |
7128 | 204 |
205 %!test | |
206 %! [result, a_idx] = ismember([1:3; 5:7; 4:6], [0:2; 1:3; 2:4; 3:5; 4:6], 'rows'); | |
207 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [2; 0; 5])); | |
208 | |
209 %!test | |
210 %! [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'); | |
211 %! assert (all (result == logical ([1; 0; 1])) && all (a_idx == [1; 0; 2])); | |
212 |