Mercurial > hg > octave-lyh
diff scripts/set/setdiff.m @ 6385:a192de8c0ead
[project @ 2007-03-06 19:05:43 by jwe]
author | jwe |
---|---|
date | Tue, 06 Mar 2007 19:05:43 +0000 |
parents | 7fad1fad19e1 |
children | 5a91bf0a47e8 |
line wrap: on
line diff
--- a/scripts/set/setdiff.m +++ b/scripts/set/setdiff.m @@ -32,20 +32,22 @@ ## Author: Paul Kienzle ## Adapted-by: jwe -function c = setdiff (a, b, byrows) +function c = setdiff (a, b, byrows_arg) if (nargin < 2 || nargin > 3) print_usage (); endif + byrows = false; + if (nargin == 3) - if (! strcmpi (byrows, "rows")) + if (! strcmpi (byrows_arg, "rows")) error ("expecting third argument to be \"rows\""); + elseif (iscell (a) || iscell (b)) + warning ("setdiff: \"rows\" not valid for cell arrays"); else byrows = true; endif - else - byrows = false; endif if (byrows) @@ -66,7 +68,11 @@ [dummy, idx] = sort ([c(:); b(:)]); ## Eliminate those elements of a that are the same as in b. n = length (dummy); - c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; + if (iscellstr (dummy)) + c(idx(find (strcmp (dummy(1:n-1), dummy(2:n))))) = []; + else + c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; + endif ## Reshape if necessary. if (size (c, 1) != 1 && size (b, 1) == 1) c = c.'; @@ -81,3 +87,5 @@ %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) +%!assert(setdiff([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 6]) +%!assert(setdiff({"one","two";"three","four"},{"one","two";"three","six"}), {"four"})