1 | function [b,ndx,pos] = unique(a,flag) |
---|
2 | % Ripped unique |
---|
3 | % For safety, we use 7.0 (6.1 sort differently...) |
---|
4 | |
---|
5 | nIn = nargin; |
---|
6 | |
---|
7 | if nIn < 1 |
---|
8 | error('MATLAB:UNIQUE:NotEnoughInputs', 'Not enough input arguments.'); |
---|
9 | elseif nIn > 2 |
---|
10 | error('MATLAB:UNIQUE:TooManyInputs', 'Too many input arguments.'); |
---|
11 | end |
---|
12 | |
---|
13 | if nIn == 1 |
---|
14 | flag = []; |
---|
15 | end |
---|
16 | |
---|
17 | rows = size(a,1); |
---|
18 | cols = size(a,2); |
---|
19 | |
---|
20 | rowvec = (rows == 1) & (cols > 1); |
---|
21 | |
---|
22 | numelA = prod(size(a)); |
---|
23 | nOut = nargout; |
---|
24 | |
---|
25 | if isempty(flag) |
---|
26 | |
---|
27 | % Handle empty: no elements. |
---|
28 | |
---|
29 | if (numelA == 0) |
---|
30 | % Predefine b to be of the correct type. |
---|
31 | b = a([]); |
---|
32 | if max(size(a)) > 0 |
---|
33 | b = reshape(b,0,1); |
---|
34 | ndx = zeros(0,1); |
---|
35 | pos = zeros(0,1); |
---|
36 | else |
---|
37 | ndx = []; |
---|
38 | pos = []; |
---|
39 | end |
---|
40 | return |
---|
41 | |
---|
42 | elseif (numelA == 1) |
---|
43 | % Scalar A: return the existing value of A. |
---|
44 | b = a; ndx = 1; pos = 1; |
---|
45 | return |
---|
46 | |
---|
47 | % General handling. |
---|
48 | else |
---|
49 | |
---|
50 | % Convert to columns |
---|
51 | a = a(:); |
---|
52 | |
---|
53 | % Convert to double array for purposes of faster sorting. |
---|
54 | % Additionally, UNIQUE calls DIFF, which requires double input. |
---|
55 | |
---|
56 | whichclass = class(a); |
---|
57 | isdouble = strcmp(whichclass,'double'); |
---|
58 | |
---|
59 | if ~isdouble |
---|
60 | a = double(a); |
---|
61 | end |
---|
62 | |
---|
63 | % Sort if unsorted. Only check this for long lists. |
---|
64 | |
---|
65 | checksortcut = 1000; |
---|
66 | |
---|
67 | if numelA <= checksortcut | ~(issorted(a)) |
---|
68 | if nOut <= 1 |
---|
69 | b = sort(a); |
---|
70 | else |
---|
71 | [b,ndx] = sort(a); |
---|
72 | end |
---|
73 | else |
---|
74 | b = a; |
---|
75 | if nOut > 1 |
---|
76 | ndx = (1:numelA)'; % If presorted, indices are 1,2,3,... |
---|
77 | end |
---|
78 | end |
---|
79 | |
---|
80 | % d indicates the location of non-matching entries. |
---|
81 | |
---|
82 | db = diff(b); |
---|
83 | |
---|
84 | % Since DIFF returns NaN in both Inf and NaN cases, |
---|
85 | % use slower method of detection if NaN's detected in DIFF(b). |
---|
86 | % After sort, Infs or NaNs will be at ends of list only. |
---|
87 | |
---|
88 | if (isnan(db(1)) | isnan(db(numelA-1))) |
---|
89 | d = b(1:numelA-1) ~= b(2:numelA); |
---|
90 | else |
---|
91 | d = db ~= 0; |
---|
92 | end |
---|
93 | |
---|
94 | d(numelA,1) = 1; % Final element is always member of unique list. |
---|
95 | |
---|
96 | b = b(d); % Create unique list by indexing into sorted list. |
---|
97 | |
---|
98 | if nOut == 3 |
---|
99 | pos = cumsum([1;full(d)]); % Lists position, starting at 1. |
---|
100 | pos(numelA+1) = []; % Remove extra element introduced by d. |
---|
101 | pos(ndx) = pos; % Re-reference POS to indexing of SORT. |
---|
102 | end |
---|
103 | |
---|
104 | % Create indices if needed. |
---|
105 | if nOut > 1 |
---|
106 | ndx = ndx(d); |
---|
107 | end |
---|
108 | |
---|
109 | % Re-convert to correct output data type using FEVAL. |
---|
110 | if ~isdouble |
---|
111 | b = feval(whichclass,b); |
---|
112 | end |
---|
113 | end |
---|
114 | |
---|
115 | % If row vector, return as row vector. |
---|
116 | if rowvec |
---|
117 | b = b.'; |
---|
118 | if nOut > 1 |
---|
119 | ndx = ndx.'; |
---|
120 | if nOut > 2 |
---|
121 | pos = pos.'; |
---|
122 | end |
---|
123 | end |
---|
124 | end |
---|
125 | |
---|
126 | else % 'rows' case |
---|
127 | if ~strcmpi(flag,'rows') |
---|
128 | error('MATLAB:UNIQUE:UnknownFlag', 'Unknown flag.'); |
---|
129 | end |
---|
130 | |
---|
131 | % Handle empty: no rows. |
---|
132 | |
---|
133 | if (rows == 0) |
---|
134 | % Predefine b to be of the correct type. |
---|
135 | b = a([]); |
---|
136 | ndx = []; |
---|
137 | pos = []; |
---|
138 | b = reshape(b,0,cols); |
---|
139 | if cols > 0 |
---|
140 | ndx = reshape(ndx,0,1); |
---|
141 | end |
---|
142 | return |
---|
143 | |
---|
144 | % Handle scalar: one row. |
---|
145 | |
---|
146 | elseif (rows == 1) |
---|
147 | b = a; ndx = 1; pos = 1; |
---|
148 | return |
---|
149 | end |
---|
150 | |
---|
151 | % General handling. |
---|
152 | % Conversion to double not done: SORTROWS is slower for doubles |
---|
153 | % than other types. |
---|
154 | |
---|
155 | if nOut > 1 |
---|
156 | [b,ndx] = sortrows(a); |
---|
157 | else |
---|
158 | b = sortrows(a); |
---|
159 | end |
---|
160 | |
---|
161 | % d indicates the location of non-matching entries. |
---|
162 | |
---|
163 | d = b(1:rows-1,:)~=b(2:rows,:); |
---|
164 | |
---|
165 | % d = 1 if differences between rows. d = 0 if the rows are equal. |
---|
166 | |
---|
167 | d = any(d,2); |
---|
168 | d(rows,1) = 1; % Final row is always member of unique list. |
---|
169 | |
---|
170 | b = b(d,:); % Create unique list by indexing into sorted list. |
---|
171 | |
---|
172 | % Create position mapping vector using CUMSUM. |
---|
173 | |
---|
174 | if nOut == 3 |
---|
175 | pos = cumsum([1;full(d)]); % Lists position, starting at 1. |
---|
176 | pos(rows+1) = []; % Remove extra element introduced by d. |
---|
177 | pos(ndx) = pos; % Re-reference POS to indexing of SORT. |
---|
178 | end |
---|
179 | |
---|
180 | % Create indices if needed. |
---|
181 | if nOut > 1 |
---|
182 | ndx = ndx(d); |
---|
183 | end |
---|
184 | end |
---|
185 | |
---|
186 | |
---|