comparison scripts/set/unique.m @ 5165:b822b4895af2

[project @ 2005-02-27 20:51:35 by dbateman]
author dbateman
date Sun, 27 Feb 2005 20:51:35 +0000
parents
children 912058eb8360
comparison
equal deleted inserted replaced
5164:57077d0ddc8e 5165:b822b4895af2
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
38 if ( nargin < 1 || nargin > 2 || (nargin == 2 && !strcmp(r,"rows")) )
39 usage ("unique (x) or unique (x, 'rows')");
40 endif
41
42 if (nargin == 1)
43 n = prod(size(x));
44 else
45 n = size(x,1);
46 endif
47
48 y = x;
49 if (n < 1)
50 i = j = [];
51 return
52 elseif (n < 2)
53 i = j = 1;
54 return
55 endif
56
57 if isstr(x), y = toascii(y); endif
58
59 if nargin == 2
60 [y, i] = sortrows(y);
61 match = all( [ y(1:n-1,:) == y(2:n,:) ]' );
62 idx = find (match);
63 y (idx, :) = [];
64 else
65 if (size(y,1) != 1) y = y(:); endif
66 [y, i] = sort(y);
67 match = [ y(1:n-1) == y(2:n) ];
68 idx = find (match);
69 y (idx) = [];
70 endif
71
72 ## I don't know why anyone would need reverse indices, but it
73 ## was an interesting challenge. I welcome cleaner solutions.
74 if (nargout >= 3)
75 j = i;
76 j (i) = cumsum ( prepad ( ~match, n, 1 ) );
77 endif
78 i (idx) = [];
79
80 if isstr(x), y = setstr(y); endif
81
82 endfunction
83
84 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2])
85 %!assert(unique([1 1 2; 1 0 1; 1 1 2],'rows'),[1 0 1; 1 1 2])
86 %!assert(unique([]),[])
87 %!assert(unique([1]),[1])
88 %!assert(unique([1 2]),[1 2])
89 %!assert(unique([1;2]),[1;2])
90 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN])