5165
|
1 ## Copyright (C) 2000-2001 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
5307
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
5165
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {} unique (@var{x}) |
|
20 ## |
|
21 ## Return the unique elements of @var{x}, sorted in ascending order. |
|
22 ## If @var{x} is a row vector, return a row vector, but if @var{x} |
|
23 ## is a column vector or a matrix return a column vector. |
|
24 ## |
|
25 ## @deftypefnx {Function File} {} unique (@var{A}, 'rows') |
|
26 ## |
|
27 ## Return the unique rows of @var{A}, sorted in ascending order. |
|
28 ## |
|
29 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] = } unique (@var{x}) |
|
30 ## |
|
31 ## Return index vectors @var{i} and @var{j} such that @code{x(i)==y} and |
|
32 ## @code{y(i)==x}. |
|
33 ## |
|
34 ## @end deftypefn |
|
35 ## @seealso{union, intersect, setdiff, setxor, ismember} |
|
36 |
|
37 function [y, i, j] = unique (x, r) |
|
38 |
5168
|
39 if (nargin < 1 || nargin > 2 || (nargin == 2 && ! strcmp (r, "rows"))) |
5165
|
40 usage ("unique (x) or unique (x, 'rows')"); |
|
41 endif |
|
42 |
|
43 if (nargin == 1) |
5168
|
44 n = prod (size (x)); |
5165
|
45 else |
5168
|
46 n = size (x, 1); |
5165
|
47 endif |
|
48 |
5168
|
49 y = x; |
5165
|
50 if (n < 1) |
|
51 i = j = []; |
5168
|
52 return; |
5165
|
53 elseif (n < 2) |
|
54 i = j = 1; |
5168
|
55 return; |
5165
|
56 endif |
|
57 |
5168
|
58 if (isstr (x)) |
|
59 y = toascii (y); |
|
60 endif |
5165
|
61 |
5168
|
62 if (nargin == 2) |
|
63 [y, i] = sortrows (y); |
5205
|
64 if (iscell (y)) |
|
65 match = cellfun ("size", y(1:n-1,:), 1) == cellfun ("size", y(2:n,:), 1); |
|
66 idx = find (match); |
|
67 match(idx) = all (char (y(idx)) == char (y(idx+1)), 2); |
|
68 match = all (match'); |
|
69 else |
|
70 match = all ([y(1:n-1,:) == y(2:n,:)]'); |
|
71 endif |
5165
|
72 idx = find (match); |
5168
|
73 y(idx,:) = []; |
5165
|
74 else |
5168
|
75 if (size (y, 1) != 1) |
|
76 y = y(:); |
|
77 endif |
|
78 [y, i] = sort (y); |
5205
|
79 if (iscell (y)) |
|
80 match = cellfun ("length", y(1:n-1)) == cellfun ("length", y(2:n)); |
|
81 idx = find(match); |
|
82 match(idx) = all (char (y(idx)) == char (y(idx+1)), 2); |
|
83 else |
|
84 match = [y(1:n-1) == y(2:n)]; |
|
85 endif |
5165
|
86 idx = find (match); |
5168
|
87 y(idx) = []; |
5165
|
88 endif |
|
89 |
|
90 ## I don't know why anyone would need reverse indices, but it |
|
91 ## was an interesting challenge. I welcome cleaner solutions. |
|
92 if (nargout >= 3) |
|
93 j = i; |
5168
|
94 j(i) = cumsum (prepad (! match, n, 1)); |
5165
|
95 endif |
5168
|
96 i(idx) = []; |
5165
|
97 |
5168
|
98 if (isstr (x)) |
|
99 y = setstr (y); |
|
100 endif |
5165
|
101 |
|
102 endfunction |
|
103 |
|
104 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2]) |
|
105 %!assert(unique([1 1 2; 1 0 1; 1 1 2],'rows'),[1 0 1; 1 1 2]) |
|
106 %!assert(unique([]),[]) |
|
107 %!assert(unique([1]),[1]) |
|
108 %!assert(unique([1 2]),[1 2]) |
|
109 %!assert(unique([1;2]),[1;2]) |
|
110 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN]) |
5205
|
111 %!assert(unique({'Foo','Bar','Foo'}),{'Bar','Foo'}) |
|
112 %!assert(unique({'Foo','Bar','FooBar'}),{'Bar','Foo','FooBar'}) |