Mercurial > hg > octave-nkf
annotate scripts/set/unique.m @ 9246:6e9ba936e983
interp3: don't require interpolation grid and data to have same size
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 14:02:02 -0400 |
parents | eb63fbe60fab |
children | 5edee330d4cb |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 Jaroslav Hajek |
7017 | 2 ## Copyright (C) 2000, 2001, 2005, 2006, 2007 Paul Kienzle |
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}) | |
8887 | 22 ## @deftypefnx {Function File} {} unique (@var{x}, "rows") |
23 ## @deftypefnx {Function File} {} unique (@dots{}, "first") | |
24 ## @deftypefnx {Function File} {} unique (@dots{}, "last") | |
25 ## @deftypefnx {Function File} {[@var{y}, @var{i}, @var{j}] =} unique (@dots{}) | |
5165 | 26 ## Return the unique elements of @var{x}, sorted in ascending order. |
27 ## If @var{x} is a row vector, return a row vector, but if @var{x} | |
28 ## is a column vector or a matrix return a column vector. | |
29 ## | |
8887 | 30 ## If the optional argument @code{"rows"} is supplied, return the unique |
31 ## rows of @var{x}, sorted in ascending order. | |
5165 | 32 ## |
8887 | 33 ## If requested, return index vectors @var{i} and @var{j} such that |
34 ## @code{x(i)==y} and @code{y(j)==x}. | |
35 ## | |
36 ## Additionally, one of @code{"first"} or @code{"last"} may be given as | |
37 ## an argument. If @code{"last"} is specified, return the highest | |
38 ## possible indices in @var{i}, otherwise, if @code{"first"} is | |
39 ## specified, return the lowest. The default is @code{"last"}. | |
5642 | 40 ## @seealso{union, intersect, setdiff, setxor, ismember} |
5165 | 41 ## @end deftypefn |
42 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
43 function [y, i, j] = unique (x, varargin) |
5165 | 44 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
45 if (nargin < 1) |
6046 | 46 print_usage (); |
5165 | 47 endif |
48 | |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
49 if (nargin > 1) |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
50 |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
51 ## parse options |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
52 if (iscellstr (varargin)) |
8413
b8de157b4948
ChangeLog entry and style fixes for last two patches
Jaroslav Hajek <highegg@gmail.com>
parents:
8412
diff
changeset
|
53 varargin = unique (varargin); |
8887 | 54 optfirst = strmatch ("first", varargin) > 0; |
55 optlast = strmatch ("last", varargin) > 0; | |
56 optrows = strmatch ("rows", varargin) > 0 && size (x, 2) > 1; | |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
57 if (optfirst && optlast) |
8664 | 58 error ("unique: cannot specify both \"last\" and \"first\""); |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
59 elseif (optfirst + optlast + optrows != nargin-1) |
8664 | 60 error ("unique: invalid option"); |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
61 endif |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
62 else |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
63 error ("unique: options must be strings"); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
64 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
65 |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
66 if (optrows && iscell (x)) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
67 warning ("unique: 'rows' is ignored for cell arrays"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
68 optrows = false; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
69 endif |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
70 |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
71 else |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
72 optfirst = 0; |
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
73 optrows = 0; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
74 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
75 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
76 if (optrows) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
77 n = size (x, 1); |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
78 dim = 1; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
79 else |
6609 | 80 n = numel (x); |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
81 dim = (size (x, 1) == 1) + 1; |
5165 | 82 endif |
83 | |
5168 | 84 y = x; |
5165 | 85 if (n < 1) |
86 i = j = []; | |
5168 | 87 return; |
5165 | 88 elseif (n < 2) |
89 i = j = 1; | |
5168 | 90 return; |
5165 | 91 endif |
92 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
93 if (optrows) |
5168 | 94 [y, i] = sortrows (y); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
95 match = all (y(1:n-1,:) == y(2:n,:), 2); |
5165 | 96 idx = find (match); |
5168 | 97 y(idx,:) = []; |
5165 | 98 else |
5168 | 99 if (size (y, 1) != 1) |
100 y = y(:); | |
101 endif | |
102 [y, i] = sort (y); | |
5205 | 103 if (iscell (y)) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
104 match = strcmp (y(1:n-1), y(2:n)); |
5205 | 105 else |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
106 match = (y(1:n-1) == y(2:n)); |
5205 | 107 endif |
5165 | 108 idx = find (match); |
5168 | 109 y(idx) = []; |
5165 | 110 endif |
111 | |
112 if (nargout >= 3) | |
113 j = i; | |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
114 if (dim == 1) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
115 j(i) = cumsum ([1; !match]); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
116 else |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
117 j(i) = cumsum ([1, !match]); |
8610
85c9906abfd1
use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents:
8502
diff
changeset
|
118 endif |
5165 | 119 endif |
8412
970b4dbff9e4
optimize unique called with a single argument
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
7920
diff
changeset
|
120 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
121 if (optfirst) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
122 i(idx+1) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
123 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
124 i(idx) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
125 endif |
5165 | 126 |
127 | |
128 endfunction | |
129 | |
130 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2]) | |
131 %!assert(unique([1 1 2; 1 0 1; 1 1 2],'rows'),[1 0 1; 1 1 2]) | |
132 %!assert(unique([]),[]) | |
133 %!assert(unique([1]),[1]) | |
134 %!assert(unique([1 2]),[1 2]) | |
135 %!assert(unique([1;2]),[1;2]) | |
136 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN]) | |
5205 | 137 %!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
|
138 %!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
|
139 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
140 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
141 %! [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
|
142 %! assert(a,[1,2,3,4]) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
143 %! assert(i,[2,3,6,7]) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
144 %! 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
|
145 %! |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7650
diff
changeset
|
146 %!test |
8502
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
147 %! [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
|
148 %! assert(a,[1,2,3,4]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
149 %! assert(i,[1,3,4,7]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
150 %! 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
|
151 %! |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
152 %!test |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
153 %! [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
|
154 %! assert(a,{'z'}) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
155 %! assert(i,[3]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
156 %! assert(j,[1,1,1]') |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
157 %! |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
158 %!test |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
159 %! 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
|
160 %! [a,i,j] = unique(A,'rows'); |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
161 %! assert(a,[1,2,3]) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
162 %! assert(A(i,:),a) |
d437e8dc18fa
make unique work for row vectors
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8413
diff
changeset
|
163 %! assert(a(j,:),A) |