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 |
5532
|
32 ## @code{y(j)==x}. |
5642
|
33 ## @seealso{union, intersect, setdiff, setxor, ismember} |
5165
|
34 ## @end deftypefn |
|
35 |
|
36 function [y, i, j] = unique (x, r) |
|
37 |
5168
|
38 if (nargin < 1 || nargin > 2 || (nargin == 2 && ! strcmp (r, "rows"))) |
6046
|
39 print_usage (); |
5165
|
40 endif |
|
41 |
|
42 if (nargin == 1) |
6609
|
43 n = numel (x); |
5165
|
44 else |
5168
|
45 n = size (x, 1); |
5165
|
46 endif |
|
47 |
5168
|
48 y = x; |
5165
|
49 if (n < 1) |
|
50 i = j = []; |
5168
|
51 return; |
5165
|
52 elseif (n < 2) |
|
53 i = j = 1; |
5168
|
54 return; |
5165
|
55 endif |
|
56 |
5443
|
57 if (ischar (x)) |
5168
|
58 y = toascii (y); |
|
59 endif |
5165
|
60 |
5737
|
61 if (nargin == 2 && size (y, 2) > 1) |
5168
|
62 [y, i] = sortrows (y); |
5205
|
63 if (iscell (y)) |
|
64 match = cellfun ("size", y(1:n-1,:), 1) == cellfun ("size", y(2:n,:), 1); |
|
65 idx = find (match); |
|
66 match(idx) = all (char (y(idx)) == char (y(idx+1)), 2); |
|
67 match = all (match'); |
|
68 else |
|
69 match = all ([y(1:n-1,:) == y(2:n,:)]'); |
|
70 endif |
5165
|
71 idx = find (match); |
5168
|
72 y(idx,:) = []; |
5165
|
73 else |
5168
|
74 if (size (y, 1) != 1) |
|
75 y = y(:); |
|
76 endif |
|
77 [y, i] = sort (y); |
5205
|
78 if (iscell (y)) |
|
79 match = cellfun ("length", y(1:n-1)) == cellfun ("length", y(2:n)); |
|
80 idx = find(match); |
|
81 match(idx) = all (char (y(idx)) == char (y(idx+1)), 2); |
|
82 else |
|
83 match = [y(1:n-1) == y(2:n)]; |
|
84 endif |
5165
|
85 idx = find (match); |
5168
|
86 y(idx) = []; |
5165
|
87 endif |
|
88 |
|
89 ## I don't know why anyone would need reverse indices, but it |
|
90 ## was an interesting challenge. I welcome cleaner solutions. |
|
91 if (nargout >= 3) |
|
92 j = i; |
5168
|
93 j(i) = cumsum (prepad (! match, n, 1)); |
5165
|
94 endif |
5168
|
95 i(idx) = []; |
5165
|
96 |
5443
|
97 if (ischar (x)) |
|
98 y = char (y); |
5168
|
99 endif |
5165
|
100 |
|
101 endfunction |
|
102 |
|
103 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2]) |
|
104 %!assert(unique([1 1 2; 1 0 1; 1 1 2],'rows'),[1 0 1; 1 1 2]) |
|
105 %!assert(unique([]),[]) |
|
106 %!assert(unique([1]),[1]) |
|
107 %!assert(unique([1 2]),[1 2]) |
|
108 %!assert(unique([1;2]),[1;2]) |
|
109 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN]) |
5205
|
110 %!assert(unique({'Foo','Bar','Foo'}),{'Bar','Foo'}) |
|
111 %!assert(unique({'Foo','Bar','FooBar'}),{'Bar','Foo','FooBar'}) |