This is a static copy of a profile reportHome
unique (6 calls, 0.031 sec)
Generated 18-Mar-2011 23:31:42 using cpu time.
M-function in file /Applications/MATLAB_R2010a.app/toolbox/matlab/ops/unique.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
59 | if ~byrow | 6 | 0.021 s | 66.7% |  |
166 | end | 2 | 0 s | 0% |  |
160 | if nOut > 1 | 2 | 0 s | 0% |  |
159 | b = b.'; | 2 | 0 s | 0% |  |
158 | if rowvec | 6 | 0 s | 0% |  |
All other lines | | | 0.010 s | 33.3% |  |
Totals | | | 0.031 s | 100% | |
Children (called functions)
No childrenM-Lint results
No M-Lint messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 368 |
Non-code lines (comments, blank lines) | 137 |
Code lines (lines that can run) | 231 |
Code lines that did run | 39 |
Code lines that did not run | 192 |
Coverage (did run/can run) | 16.88 % |
Function listing
time calls line
1 function [b,ndx,pos] = unique(a,flag1,flag2)
2 %UNIQUE Set unique.
3 % B = UNIQUE(A) for the array A returns the same values as in A but
4 % with no repetitions. B will also be sorted. A can be a cell array of
5 % strings.
6 %
7 % UNIQUE(A,'rows') for the matrix A returns the unique rows of A.
8 %
9 % [B,I,J] = UNIQUE(...) also returns index vectors I and J such
10 % that B = A(I) and A = B(J) (or B = A(I,:) and A = B(J,:)).
11 %
12 % [B,I,J] = UNIQUE(...,'first') returns the vector I to index the
13 % first occurrence of each unique value in A. UNIQUE(...,'last'),
14 % the default, returns the vector I to index the last occurrence.
15 %
16 % See also UNION, INTERSECT, SETDIFF, SETXOR, ISMEMBER, SORT, ISSORTED.
17
18 % Copyright 1984-2009 The MathWorks, Inc.
19 % $Revision: 1.24.4.8 $ $Date: 2009/09/28 20:28:10 $
20
21 % Cell array implementation in @cell/unique.m
22
6 23 flagvals = {'rows' 'first' 'last'};
6 24 if nargin > 1
25 options = strcmpi(flag1,flagvals);
26 if nargin > 2
27 options = options + strcmpi(flag2,flagvals);
28 if any(options > 1) || (options(2)+options(3) > 1)
29 error('MATLAB:UNIQUE:RepeatedFlag', ...
30 'You may not specify more than one value for the same option.');
31 end
32 end
33 if sum(options) < nargin-1
34 error('MATLAB:UNIQUE:UnknownFlag', 'Unrecognized option.');
35 end
36 byrow = (options(1) > 0);
37 if options(2) > 0
38 order = 'first';
39 else % if options(3) > 0 || sum(options(2:3) == 0)
40 order = 'last';
41 end
6 42 elseif nargin == 1
6 43 byrow = false;
6 44 order = 'last';
45 else
46 error('MATLAB:UNIQUE:NotEnoughInputs', 'Not enough input arguments.');
47 end
48
6 49 rows = size(a,1);
6 50 cols = size(a,2);
51
6 52 rowvec = (rows == 1) && (cols > 1);
53
6 54 numelA = numel(a);
6 55 nOut = nargout;
56
6 57 if ~isa(a,'opaque')
58
0.02 6 59 if ~byrow
60
61 % Handle empty: no elements.
62
6 63 if (numelA == 0)
64 % Predefine b to be of the correct type.
65 b = a([]);
66 if max(size(a)) > 0
67 b = reshape(b,0,1);
68 ndx = zeros(0,1);
69 pos = zeros(0,1);
70 else
71 ndx = [];
72 pos = [];
73 end
74 return
75
6 76 elseif (numelA == 1)
77 % Scalar A: return the existing value of A.
78 b = a; ndx = 1; pos = 1;
79 return
80
81 % General handling.
6 82 else
83
84 % Convert to columns
6 85 a = a(:);
86
87 % Convert to double array for purposes of faster sorting.
88 % Additionally, UNIQUE calls DIFF, which requires double input.
89
6 90 whichclass = class(a);
6 91 isdouble = strcmp(whichclass,'double');
92
6 93 if ~isdouble
94 a = double(a);
95 end
96
97 % Sort if unsorted. Only check this for long lists.
98
6 99 checksortcut = 1000;
100
6 101 if numelA <= checksortcut || ~(issorted(a))
6 102 if nOut <= 1
6 103 b = sort(a);
104 else
105 [b,ndx] = sort(a);
106 end
107 else
108 b = a;
109 if nOut > 1
110 ndx = (1:numelA)'; % If presorted, indices are 1,2,3,...
111 end
112 end
113
114 % d indicates the location of non-matching entries.
115
6 116 db = diff(b);
117
118 % Since DIFF returns NaN in both Inf and NaN cases,
119 % use slower method of detection if NaN's detected in DIFF(b).
120 % After sort, Infs or NaNs will be at ends of list only.
121
6 122 if (isnan(db(1)) || isnan(db(numelA-1)))
123 d = b(1:numelA-1) ~= b(2:numelA);
6 124 else
6 125 d = db ~= 0;
6 126 end
127
6 128 if order(1) == 'l' % 'last'
6 129 d(numelA,1) = true; % Final element is always a member of unique list.
130 else % order == 'first'
131 d = [true; d]; % First element is always a member of unique list.
132 end
133
6 134 b = b(d); % Create unique list by indexing into sorted list.
135
6 136 if nOut == 3
137 if order(1) == 'l' % 'last'
138 pos = cumsum([1;full(d)]); % Lists position, starting at 1.
139 pos(numelA+1) = []; % Remove extra element introduced by d.
140 else % order == 'first'
141 pos = cumsum(full(d)); % Lists position, starting at 1.
142 end
143 pos(ndx) = pos; % Re-reference POS to indexing of SORT.
144 end
145
146 % Create indices if needed.
6 147 if nOut > 1
148 ndx = ndx(d);
149 end
150
151 % Re-convert to correct output data type using FEVAL.
6 152 if ~isdouble
153 b = feval(whichclass,b);
154 end
6 155 end
156
157 % If row vector, return as row vector.
6 158 if rowvec
2 159 b = b.';
2 160 if nOut > 1
161 ndx = ndx.';
162 if nOut > 2
163 pos = pos.';
164 end
165 end
2 166 end
167
168 else % 'rows' case
169
170 % Handle empty: no rows.
171
172 if (rows == 0)
173 % Predefine b to be of the correct type.
174 b = a([]);
175 ndx = [];
176 pos = [];
177 b = reshape(b,0,cols);
178 if cols > 0
179 ndx = reshape(ndx,0,1);
180 end
181 return
182
183 % Handle scalar: one row.
184
185 elseif (rows == 1)
186 b = a; ndx = 1; pos = 1;
187 return
188 end
189
190 % General handling.
191 % Conversion to double not done: SORTROWS is slower for doubles
192 % than other types.
193
194 if nOut > 1
195 [b,ndx] = sortrows(a);
196 else
197 b = sortrows(a);
198 end
199
200 % d indicates the location of non-matching entries.
201
202 d = b(1:rows-1,:)~=b(2:rows,:);
203
204 % d = 1 if differences between rows. d = 0 if the rows are equal.
205
206 d = any(d,2);
207 if order(1) == 'l' % 'last'
208 d(rows,1) = true; % Final row is always member of unique list.
209 else % order = 'first'
210 d = [true; d]; % First row is always a member of unique list.
211 end
212
213 b = b(d,:); % Create unique list by indexing into sorted list.
214
215 % Create position mapping vector using CUMSUM.
216
217 if nOut == 3
218 if order(1) == 'l' % 'last'
219 pos = cumsum([1;full(d)]); % Lists position, starting at 1.
220 pos(rows+1) = []; % Remove extra element introduced by d.
221 else % order == 'first'
222 pos = cumsum(full(d)); % Lists position, starting at 1.
223 end
224 pos(ndx) = pos; % Re-reference POS to indexing of SORT.
225 end
226
227 % Create indices if needed.
228 if nOut > 1
229 ndx = ndx(d);
230 end
231 end
232
233 else
234 % Handle objects that cannot be converted to doubles
235 if ~byrow
236
237 % Handle empty: no elements.
238
239 if (numelA == 0)
240 % Predefine b to be of the correct type.
241 b = a([]);
242 if max(size(a)) > 0
243 b = reshape(b,0,1);
244 ndx = zeros(0,1);
245 pos = zeros(0,1);
246 else
247 ndx = [];
248 pos = [];
249 end
250 return
251
252 elseif (numelA == 1)
253 % Scalar A: return the existing value of A.
254 b = a; ndx = 1; pos = 1;
255 return
256
257 % General handling.
258 else
259
260 % Convert to columns
261 a = a(:);
262
263 % Sort if unsorted. Only check this for long lists.
264
265 if nOut <= 1
266 b = sort(a);
267 else
268 [b,ndx] = sort(a);
269 end
270
271 % d indicates the location of non-matching entries.
272
273 d = b(1:numelA-1) ~= b(2:numelA);
274
275 if order(1) == 'l' % 'last'
276 d(numelA,1) = true; % Final element is always a member of unique list.
277 else % order == 'first'
278 d = [true; d]; % First element is always a member of unique list.
279 end
280
281 b = b(d); % Create unique list by indexing into sorted list.
282
283 if nOut == 3
284 if order(1) == 'l' % 'last'
285 pos = cumsum([1;d]); % Lists position, starting at 1.
286 pos(numelA+1) = []; % Remove extra element introduced by d.
287 else % order == 'first'
288 pos = cumsum(d); % Lists position, starting at 1.
289 end
290 pos(ndx) = pos; % Re-reference POS to indexing of SORT.
291 end
292
293 % Create indices if needed.
294 if nOut > 1
295 ndx = ndx(d);
296 end
297 end
298
299 % If row vector, return as row vector.
300 if rowvec
301 b = b.';
302 if nOut > 1
303 ndx = ndx.';
304 if nOut > 2
305 pos = pos.';
306 end
307 end
308 end
309
310 else % 'rows' case
311
312 % Handle empty: no rows.
313
314 if (rows == 0)
315 % Predefine b to be of the correct type.
316 b = a([]);
317 ndx = [];
318 pos = [];
319 b = reshape(b,0,cols);
320 if cols > 0
321 ndx = reshape(ndx,0,1);
322 end
323 return
324
325 % Handle scalar: one row.
326
327 elseif (rows == 1)
328 b = a; ndx = 1; pos = 1;
329 return
330 end
331
332 % General handling.
333
334 if nOut > 1
335 [b,ndx] = sortrows(a);
336 else
337 b = sortrows(a);
338 end
339
340 % d indicates the location of non-matching entries.
341
342 d = b(1:rows-1,:)~=b(2:rows,:);
343
344 % d = 1 if differences between rows. d = 0 if the rows are equal.
345
346 d = any(d,2);
347 if order(1) == 'l' % 'last'
348 d(rows,1) = true; % Final row is always member of unique list.
349 else % order == 'first'
350 d = [true; d]; % First row is always a member of unique list.
351 end
352
353 b = b(d,:); % Create unique list by indexing into sorted list.
354
355 % Create position mapping vector using CUMSUM.
356
357 if nOut == 3
358 pos = cumsum([1;d]); % Lists position, starting at 1.
359 pos(rows+1) = []; % Remove extra element introduced by d.
360 pos(ndx) = pos; % Re-reference POS to indexing of SORT.
361 end
362
363 % Create indices if needed.
364 if nOut > 1
365 ndx = ndx(d);
366 end
367 end
368 end