[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: proposed patch for ismember.m
From: |
John W. Eaton |
Subject: |
Re: proposed patch for ismember.m |
Date: |
Wed, 07 Nov 2007 23:05:05 -0500 |
On 7-Nov-2007, Ben Abbott wrote:
| On Nov 7, 2007, at 3:37 PM, John W. Eaton wrote:
|
| > On 6-Nov-2007, Ben Abbott wrote:
| >
| > | I started with Søren's recent work on ismember.m, and believe we now
| > | have a Matlab compatible implementation of ismember.m
| > |
| > | The texinfo has been updated and several new tests have been added.
| > |
| > | I've attached the patch (which I hope is constructed properly) and a
| > | ChangeLog.
| > |
| > | In the event, the patch doesn't take, please give me explicit
| > | instructions for how it should be created. I created the patch using
| > |
| > | cvs diff -u > ismember.patch
| > |
| > | from the root directory of my CVS copy of octave.
| >
| > I didn't look closely at the patch, but in order to simplify things, I
| > think I would prefer to have a version of this function that uses the
| > form I outlined here:
| >
| > https://www.cae.wisc.edu/pipermail/bug-octave/2007-November/
| > 003789.html
| >
| > It would simplify things and also once we have objects, we can move
| > the cell_ismember function to @cell/ismember.m and let Octave do the
| > dispatching.
| >
| > jwe
|
| Please take a look at this patch, and let me know if the separation
| if sufficient.
I applied this patch and checked it in with the following additional
changes.
Thanks,
jwe
Index: scripts/set/ismember.m
===================================================================
RCS file: /cvs/octave/scripts/set/ismember.m,v
retrieving revision 1.19
diff -u -u -r1.19 ismember.m
--- scripts/set/ismember.m 8 Nov 2007 03:55:04 -0000 1.19
+++ scripts/set/ismember.m 8 Nov 2007 04:04:26 -0000
@@ -18,9 +18,10 @@
## -*- texinfo -*-
## @deftypefn {Function File} address@hidden, @var{a_idx}] =} ismember
(@var{A}, @var{S})
+## @deftypefnx {Function File} address@hidden, @var{a_idx}] =} ismember
(@var{A}, @var{S}, "rows")
## Return a matrix @var{tf} the same shape as @var{A} which has 1 if
## @code{A(i,j)} is in @var{S} or 0 if it isn't. If a second output argument
-## is requested, the indexes into @var{S} of the matching elements is
+## is requested, the indexes into @var{S} of the matching elements are
## also returned.
##
## @example
@@ -45,9 +46,9 @@
## @end group
## @end example
##
-## @deftypefnx {Function File} address@hidden, @var{a_idx}] =} ismember
(@var{A}, @var{S}, 'rows')
-## When @var{A} and @var{S} are matrices with the same number of columes,
-## the row vectors may matched.
+## With the optional third argument @code{"rows"}, and matrices
+## @var{A} and @var{S} with the same number of columns, compare rows in
+## @var{A} with the rows in @var{S}.
##
## @example
## @group
@@ -62,15 +63,15 @@
## @seealso{unique, union, intersection, setxor, setdiff}
## @end deftypefn
-## Author: Paul Kienzle
-## Author: Søren Hauberg
-## Author: Ben Abbott
+## Author: Paul Kienzle <address@hidden>
+## Author: Søren Hauberg <address@hidden>
+## Author: Ben Abbott <address@hidden>
## Adapted-by: jwe
function [tf, a_idx] = ismember (a, s, rows_opt)
if (nargin == 2 || nargin == 3)
- if (iscell(a) || iscell(s))
+ if (iscell (a) || iscell (s))
if (nargin == 3)
error ("ismember: with 'rows' both sets must be matrices");
else
@@ -80,42 +81,42 @@
if (nargin == 3)
## The 'rows' argument is handled in a fairly ugly way. A better
## solution would be to vectorize this loop over 'r' below.
- if (strcmpi (rows_opt, "rows") && ismatrix (a) && ismatrix (s) && ...
- columns (a) == columns (s))
+ if (strcmpi (rows_opt, "rows") && ismatrix (a) && ismatrix (s)
+ && columns (a) == columns (s))
rs = rows (s);
ra = rows (a);
a_idx = zeros (ra, 1);
for r = 1:ra
tmp = ones (rs, 1) * a(r,:);
f = find (all (tmp' == s'), 1);
- if ! isempty (f)
+ if (! isempty (f))
a_idx(r) = f;
endif
endfor
tf = logical (a_idx);
- elseif strcmpi (rows_opt, "rows")
+ elseif (strcmpi (rows_opt, "rows"))
error ("ismember: with 'rows' both sets must be matrices with an
equal number of columns");
else
error ("ismember: invalid input");
endif
else
## Input checking
- if ( ! isa (a, class (s)) )
+ if (! isa (a, class (s)))
error ("ismember: both input arguments must be the same type");
- elseif ( ! ischar (a) && ! isnumeric (a) )
+ elseif (! ischar (a) && ! isnumeric (a))
error ("ismember: input arguments must be arrays, cell arrays, or
strings");
- elseif ( ischar (a) && ischar (s) )
+ elseif (ischar (a) && ischar (s))
a = uint8 (a);
s = uint8 (s);
endif
- ## Convert matrices to vectors
+ ## Convert matrices to vectors.
if (all (size (a) > 1))
a = a(:);
endif
if (all (size (s) > 1))
s = s(:);
endif
- ## Do the actual work
+ ## Do the actual work.
if (isempty (a) || isempty (s))
tf = zeros (size (a), "logical");
a_idx = [];
@@ -124,7 +125,7 @@
a_idx = double (tf);
elseif (numel (a) == 1)
f = find (a == s, 1);
- tf = !isempty (f);
+ tf = !isempty (f);
a_idx = f;
if (isempty (a_idx))
a_idx = 0;
@@ -186,13 +187,13 @@
if (isempty (a)) # Work around bug in 'cellstr'
a = {''};
else
- a = cellstr(a);
+ a = cellstr (a);
endif
elseif (iscellstr (a) && ischar (s))
if (isempty (s)) # Work around bug in 'cellstr'
s = {''};
else
- s = cellstr(s);
+ s = cellstr (s);
endif
endif
if (iscellstr (a) && iscellstr (s))
@@ -211,15 +212,15 @@
a_idx = 0;
endif
else
- lt = numel(s);
+ lt = numel (s);
[s, sidx] = sort (s);
[v, p] = sort ([s(2:lt)(:); a(:)]);
idx(p) = cumsum (p <= lt-1) + 1;
idx = idx(lt:end);
tf = (cellfun ("length", a)
- == reshape (cellfun ("length", s(idx)), size (a)));
+ == reshape (cellfun ("length", s(idx)), size (a)));
idx2 = find (tf);
- tf(idx2) = all (char (a(idx2)) == char (s(idx)(idx2)), 2);
+ tf(idx2) = (all (char (a(idx2)) == char (s(idx)(idx2)), 2));
a_idx = zeros (size (tf));
a_idx(tf) = sidx(idx(tf));
endif
@@ -230,7 +231,7 @@
print_usage ();
endif
## Resize result to the original size of 'a'
- size_a = size(a);
+ size_a = size (a);
tf = reshape (tf, size_a);
a_idx = reshape (a_idx, size_a);
endfunction
- Re: problem with sum (...), (continued)
- Re: problem with sum (...), Ben Abbott, 2007/11/04
- Re: problem with sum (...), Ben Abbott, 2007/11/04
- Re: problem with sum (...), Ben Abbott, 2007/11/04
- Re: problem with sum (...), Søren Hauberg, 2007/11/05
- Re: problem with sum (...), Ben Abbott, 2007/11/05
- proposed patch for ismember.m, Ben Abbott, 2007/11/06
- proposed patch for ismember.m, John W. Eaton, 2007/11/07
- Re: proposed patch for ismember.m, Ben Abbott, 2007/11/07
- Re: proposed patch for ismember.m, John W. Eaton, 2007/11/07
- Re: proposed patch for ismember.m, Ben Abbott, 2007/11/07
- Re: proposed patch for ismember.m,
John W. Eaton <=
- Re: proposed patch for ismember.m, Ben Abbott, 2007/11/08
Re: problem with sum (...), David Bateman, 2007/11/05
Re: problem with sum (...), David Bateman, 2007/11/06
Re: problem with sum (...), Ben Abbott, 2007/11/05
Re: problem with sum (...), John W. Eaton, 2007/11/06