This is a static copy of a profile report

Home

unique (294 calls, 0.098 sec)
Generated 05-Aug-2011 13:00:52 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/ops/unique.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
interpRegistersfunction3
flagMaskfunction1
calcSourceDistfunction3
setdifffunction232
linkaxesfunction1
scribe.legend.methods>lscansubfunction18
detrendfunction36
Lines where the most time was spent

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