Mercurial > hg > octave-nkf
comparison 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 |
comparison
equal
deleted
inserted
replaced
6384:c2eb95ca0e2b | 6385:a192de8c0ead |
---|---|
30 ## @end deftypefn | 30 ## @end deftypefn |
31 | 31 |
32 ## Author: Paul Kienzle | 32 ## Author: Paul Kienzle |
33 ## Adapted-by: jwe | 33 ## Adapted-by: jwe |
34 | 34 |
35 function c = setdiff (a, b, byrows) | 35 function c = setdiff (a, b, byrows_arg) |
36 | 36 |
37 if (nargin < 2 || nargin > 3) | 37 if (nargin < 2 || nargin > 3) |
38 print_usage (); | 38 print_usage (); |
39 endif | 39 endif |
40 | 40 |
41 byrows = false; | |
42 | |
41 if (nargin == 3) | 43 if (nargin == 3) |
42 if (! strcmpi (byrows, "rows")) | 44 if (! strcmpi (byrows_arg, "rows")) |
43 error ("expecting third argument to be \"rows\""); | 45 error ("expecting third argument to be \"rows\""); |
46 elseif (iscell (a) || iscell (b)) | |
47 warning ("setdiff: \"rows\" not valid for cell arrays"); | |
44 else | 48 else |
45 byrows = true; | 49 byrows = true; |
46 endif | 50 endif |
47 else | |
48 byrows = false; | |
49 endif | 51 endif |
50 | 52 |
51 if (byrows) | 53 if (byrows) |
52 c = unique (a, "rows"); | 54 c = unique (a, "rows"); |
53 if (! isempty (c) && ! isempty (b)) | 55 if (! isempty (c) && ! isempty (b)) |
64 ## Form a and b into combined set. | 66 ## Form a and b into combined set. |
65 b = unique (b); | 67 b = unique (b); |
66 [dummy, idx] = sort ([c(:); b(:)]); | 68 [dummy, idx] = sort ([c(:); b(:)]); |
67 ## Eliminate those elements of a that are the same as in b. | 69 ## Eliminate those elements of a that are the same as in b. |
68 n = length (dummy); | 70 n = length (dummy); |
69 c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; | 71 if (iscellstr (dummy)) |
72 c(idx(find (strcmp (dummy(1:n-1), dummy(2:n))))) = []; | |
73 else | |
74 c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; | |
75 endif | |
70 ## Reshape if necessary. | 76 ## Reshape if necessary. |
71 if (size (c, 1) != 1 && size (b, 1) == 1) | 77 if (size (c, 1) != 1 && size (b, 1) == 1) |
72 c = c.'; | 78 c = c.'; |
73 endif | 79 endif |
74 endif | 80 endif |
79 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") | 85 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") |
80 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") | 86 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") |
81 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") | 87 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") |
82 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) | 88 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) |
83 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) | 89 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) |
90 %!assert(setdiff([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 6]) | |
91 %!assert(setdiff({"one","two";"three","four"},{"one","two";"three","six"}), {"four"}) |