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 |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} unique (@var{x}) |
|
19 ## |
|
20 ## Return the unique elements of @var{x}, sorted in ascending order. |
|
21 ## If @var{x} is a row vector, return a row vector, but if @var{x} |
|
22 ## is a column vector or a matrix return a column vector. |
|
23 ## |
|
24 ## @deftypefnx {Function File} {} unique (@var{A}, 'rows') |
|
25 ## |
|
26 ## Return the unique rows of @var{A}, sorted in ascending order. |
|
27 ## |
|
28 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] = } unique (@var{x}) |
|
29 ## |
|
30 ## Return index vectors @var{i} and @var{j} such that @code{x(i)==y} and |
|
31 ## @code{y(i)==x}. |
|
32 ## |
|
33 ## @end deftypefn |
|
34 ## @seealso{union, intersect, setdiff, setxor, ismember} |
|
35 |
|
36 function [y, i, j] = unique (x, r) |
|
37 |
5168
|
38 if (nargin < 1 || nargin > 2 || (nargin == 2 && ! strcmp (r, "rows"))) |
5165
|
39 usage ("unique (x) or unique (x, 'rows')"); |
|
40 endif |
|
41 |
|
42 if (nargin == 1) |
5168
|
43 n = prod (size (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 |
5168
|
57 if (isstr (x)) |
|
58 y = toascii (y); |
|
59 endif |
5165
|
60 |
5168
|
61 if (nargin == 2) |
|
62 [y, i] = sortrows (y); |
|
63 match = all ((y(1:n-1,:) == y(2:n,:))'); |
5165
|
64 idx = find (match); |
5168
|
65 y(idx,:) = []; |
5165
|
66 else |
5168
|
67 if (size (y, 1) != 1) |
|
68 y = y(:); |
|
69 endif |
|
70 [y, i] = sort (y); |
|
71 match = y(1:n-1) == y(2:n); |
5165
|
72 idx = find (match); |
5168
|
73 y(idx) = []; |
5165
|
74 endif |
|
75 |
|
76 ## I don't know why anyone would need reverse indices, but it |
|
77 ## was an interesting challenge. I welcome cleaner solutions. |
|
78 if (nargout >= 3) |
|
79 j = i; |
5168
|
80 j(i) = cumsum (prepad (! match, n, 1)); |
5165
|
81 endif |
5168
|
82 i(idx) = []; |
5165
|
83 |
5168
|
84 if (isstr (x)) |
|
85 y = setstr (y); |
|
86 endif |
5165
|
87 |
|
88 endfunction |
|
89 |
|
90 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2]) |
|
91 %!assert(unique([1 1 2; 1 0 1; 1 1 2],'rows'),[1 0 1; 1 1 2]) |
|
92 %!assert(unique([]),[]) |
|
93 %!assert(unique([1]),[1]) |
|
94 %!assert(unique([1 2]),[1 2]) |
|
95 %!assert(unique([1;2]),[1;2]) |
|
96 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN]) |