Mercurial > octave-nkf
annotate scripts/set/unique.m @ 8502:d437e8dc18fa
make unique work for row vectors
author | Daniel J Sebald <daniel.sebald@ieee.org> |
---|---|
date | Tue, 13 Jan 2009 09:34:43 +0100 |
parents | b8de157b4948 |
children | 85c9906abfd1 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2000, 2001, 2005, 2006, 2007 Paul Kienzle |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
2 ## Copyright (C) 2008 Jaroslav Hajek |
7016 | 3 ## |
4 ## This file is part of Octave. | |
5165 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
5165 | 10 ## |
7016 | 11 ## Octave is distributed in the hope that it will be useful, but |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
5165 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5165 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} unique (@var{x}) | |
22 ## | |
23 ## Return the unique elements of @var{x}, sorted in ascending order. | |
24 ## If @var{x} is a row vector, return a row vector, but if @var{x} | |
25 ## is a column vector or a matrix return a column vector. | |
26 ## | |
27 ## @deftypefnx {Function File} {} unique (@var{A}, 'rows') | |
28 ## | |
29 ## Return the unique rows of @var{A}, sorted in ascending order. | |
30 ## | |
7650 | 31 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] =} unique (@var{x}) |
5165 | 32 ## |
33 ## Return index vectors @var{i} and @var{j} such that @code{x(i)==y} and | |
5532 | 34 ## @code{y(j)==x}. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
35 ## |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
36 ## Additionally, one of 'first' or 'last' can be given as an argument. |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
37 ## 'last' (default) specifies that the highest possible indices are returned |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
38 ## in @var{i}, while 'first' means the lowest. |
5642 | 39 ## @seealso{union, intersect, setdiff, setxor, ismember} |
5165 | 40 ## @end deftypefn |
41 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
42 function [y, i, j] = unique (x, varargin) |
5165 | 43 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
44 if (nargin < 1) |
6046 | 45 print_usage (); |
5165 | 46 endif |
47 | |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
48 if (nargin > 1) |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
49 |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
50 ## parse options |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
51 if (iscellstr (varargin)) |
8413
b8de157b4948
ChangeLog entry and style fixes for last two patches
Jaroslav Hajek <highegg@gmail.com>
parents:
8412
diff
changeset
|
52 varargin = unique (varargin); |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
53 optfirst = strmatch ('first', varargin) > 0; |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
54 optlast = strmatch ('last', varargin) > 0; |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
55 optrows = strmatch ('rows', varargin) > 0 && size (x, 2) > 1; |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
56 if (optfirst && optlast) |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
57 error ("unique: cannot specify both 'last' and 'first'."); |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
58 elseif (optfirst + optlast + optrows != nargin-1) |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
59 error ("unique: invalid option."); |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
60 endif |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
61 else |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
62 error ("unique: options must be strings"); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
63 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
64 |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
65 if (optrows && iscell (x)) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
66 warning ("unique: 'rows' is ignored for cell arrays"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
67 optrows = false; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
68 endif |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
69 |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
70 else |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
71 optfirst = 0; |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
72 optrows = 0; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
73 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
74 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
75 if (optrows) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
76 n = size (x, 1); |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
77 dim = 1; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
78 else |
6609 | 79 n = numel (x); |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
80 dim = (size (x, 1) == 1) + 1; |
5165 | 81 endif |
82 | |
5168 | 83 y = x; |
5165 | 84 if (n < 1) |
85 i = j = []; | |
5168 | 86 return; |
5165 | 87 elseif (n < 2) |
88 i = j = 1; | |
5168 | 89 return; |
5165 | 90 endif |
91 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
92 if (optrows) |
5168 | 93 [y, i] = sortrows (y); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
94 match = all (y(1:n-1,:) == y(2:n,:), 2); |
5165 | 95 idx = find (match); |
5168 | 96 y(idx,:) = []; |
5165 | 97 else |
5168 | 98 if (size (y, 1) != 1) |
99 y = y(:); | |
100 endif | |
101 [y, i] = sort (y); | |
5205 | 102 if (iscell (y)) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
103 match = strcmp (y(1:n-1), y(2:n)); |
5205 | 104 else |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
105 match = (y(1:n-1) == y(2:n)); |
5205 | 106 endif |
5165 | 107 idx = find (match); |
5168 | 108 y(idx) = []; |
5165 | 109 endif |
110 | |
111 if (nargout >= 3) | |
112 j = i; | |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
113 if (dim == 1) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
114 j(i) = cumsum ([1; !match]); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
115 else |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
116 j(i) = cumsum ([1, !match]); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
117 end |
5165 | 118 endif |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
119 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
120 if (optfirst) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
121 i(idx+1) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
122 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
123 i(idx) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
124 endif |
5165 | 125 |
126 | |
127 endfunction | |
128 | |
129 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2]) | |
130 %!assert(unique([1 1 2; 1 0 1; 1 1 2],'rows'),[1 0 1; 1 1 2]) | |
131 %!assert(unique([]),[]) | |
132 %!assert(unique([1]),[1]) | |
133 %!assert(unique([1 2]),[1 2]) | |
134 %!assert(unique([1;2]),[1;2]) | |
135 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN]) | |
5205 | 136 %!assert(unique({'Foo','Bar','Foo'}),{'Bar','Foo'}) |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
137 %!assert(unique({'Foo','Bar','FooBar'}'),{'Bar','Foo','FooBar'}') |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
138 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
139 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
140 %! [a,i,j] = unique([1,1,2,3,3,3,4]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
141 %! assert(a,[1,2,3,4]) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
142 %! assert(i,[2,3,6,7]) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
143 %! assert(j,[1,1,2,3,3,3,4]) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
144 %! |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
145 %!test |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
146 %! [a,i,j] = unique([1,1,2,3,3,3,4]','first'); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
147 %! assert(a,[1,2,3,4]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
148 %! assert(i,[1,3,4,7]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
149 %! assert(j,[1,1,2,3,3,3,4]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
150 %! |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
151 %!test |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
152 %! [a,i,j] = unique({'z'; 'z'; 'z'}); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
153 %! assert(a,{'z'}) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
154 %! assert(i,[3]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
155 %! assert(j,[1,1,1]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
156 %! |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
157 %!test |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
158 %! A=[1,2,3;1,2,3]; |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
159 %! [a,i,j] = unique(A,'rows'); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
160 %! assert(a,[1,2,3]) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
161 %! assert(A(i,:),a) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
162 %! assert(a(j,:),A) |