This is a static copy of a profile report

Home

unique (69 calls, 0.016 sec)
Generated 15-Mar-2007 12:01:46 using real time.
M-function in file C:\Program Files\MATLAB71\toolbox\matlab\ops\unique.m
[Copy to new window for comparing multiple runs]

Parents (calling functions)

Function NameFunction TypeCalls
setdiffM-function69
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
108
d(numelA,1) = 1;    % Final el...
260.000 s1.3%
28
flag = [];
690.000 s0.8%
58
b = a; ndx = 1; pos = 1;
430.000 s0.7%
19
nIn = nargin;
690.000 s0.1%
37
nOut = nargout;
690.000 s0.0%
Other lines & overhead  0.015 s97.1%
Totals  0.016 s100% 
Children (called functions)
No children
M-Lint results
No M-Lint messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in file198
Non-code lines (comments, blank lines)85
Code lines (lines that can run)113
Code lines that did run37
Code lines that did not run76
Coverage (did run/can run)32.74 %
Function listing
   time   calls  line
1 function [b,ndx,pos] = unique(a,flag)
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 % See also UNION, INTERSECT, SETDIFF, SETXOR, ISMEMBER.
13
14 % Copyright 1984-2004 The MathWorks, Inc.
15 % $Revision: 1.24.4.4 $ $Date: 2004/12/06 16:35:44 $
16
17 % Cell array implementation in @cell/unique.m
18
< 0.01 69 19 nIn = nargin;
20
69 21 if nIn < 1
22 error('MATLAB:UNIQUE:NotEnoughInputs', 'Not enough input arguments.');
69 23 elseif nIn > 2
24 error('MATLAB:UNIQUE:TooManyInputs', 'Too many input arguments.');
25 end
26
< 0.01 69 27 if nIn == 1
< 0.01 69 28 flag = [];
< 0.01 69 29 end
30
< 0.01 69 31 rows = size(a,1);
< 0.01 69 32 cols = size(a,2);
33
< 0.01 69 34 rowvec = (rows == 1) && (cols > 1);
35
< 0.01 69 36 numelA = numel(a);
< 0.01 69 37 nOut = nargout;
38
69 39 if isempty(flag)
40
41 % Handle empty: no elements.
42
69 43 if (numelA == 0)
44 % Predefine b to be of the correct type.
45 b = a([]);
46 if max(size(a)) > 0
47 b = reshape(b,0,1);
48 ndx = zeros(0,1);
49 pos = zeros(0,1);
50 else
51 ndx = [];
52 pos = [];
53 end
54 return
55
69 56 elseif (numelA == 1)
57 % Scalar A: return the existing value of A.
< 0.01 43 58 b = a; ndx = 1; pos = 1;
43 59 return
60
61 % General handling.
26 62 else
63
64 % Convert to columns
26 65 a = a(:);
66
67 % Convert to double array for purposes of faster sorting.
68 % Additionally, UNIQUE calls DIFF, which requires double input.
69
26 70 whichclass = class(a);
26 71 isdouble = strcmp(whichclass,'double');
72
26 73 if ~isdouble
74 a = double(a);
75 end
76
77 % Sort if unsorted. Only check this for long lists.
78
< 0.01 26 79 checksortcut = 1000;
80
26 81 if numelA <= checksortcut || ~(issorted(a))
26 82 if nOut <= 1
26 83 b = sort(a);
84 else
85 [b,ndx] = sort(a);
86 end
87 else
88 b = a;
89 if nOut > 1
90 ndx = (1:numelA)'; % If presorted, indices are 1,2,3,...
91 end
92 end
93
94 % d indicates the location of non-matching entries.
95
26 96 db = diff(b);
97
98 % Since DIFF returns NaN in both Inf and NaN cases,
99 % use slower method of detection if NaN's detected in DIFF(b).
100 % After sort, Infs or NaNs will be at ends of list only.
101
26 102 if (isnan(db(1)) || isnan(db(numelA-1)))
103 d = b(1:numelA-1) ~= b(2:numelA);
< 0.01 26 104 else
26 105 d = db ~= 0;
26 106 end
107
< 0.01 26 108 d(numelA,1) = 1; % Final element is always member of unique list.
109
26 110 b = b(d); % Create unique list by indexing into sorted list.
111
26 112 if nOut == 3
113 pos = cumsum([1;full(d)]); % Lists position, starting at 1.
114 pos(numelA+1) = []; % Remove extra element introduced by d.
115 pos(ndx) = pos; % Re-reference POS to indexing of SORT.
116 end
117
118 % Create indices if needed.
26 119 if nOut > 1
120 ndx = ndx(d);
121 end
122
123 % Re-convert to correct output data type using FEVAL.
26 124 if ~isdouble
125 b = feval(whichclass,b);
126 end
26 127 end
128
129 % If row vector, return as row vector.
26 130 if rowvec
131 b = b.';
132 if nOut > 1
133 ndx = ndx.';
134 if nOut > 2
135 pos = pos.';
136 end
137 end
138 end
139
140 else % 'rows' case
141 if ~strcmpi(flag,'rows')
142 error('MATLAB:UNIQUE:UnknownFlag', 'Unknown flag.');
143 end
144
145 % Handle empty: no rows.
146
147 if (rows == 0)
148 % Predefine b to be of the correct type.
149 b = a([]);
150 ndx = [];
151 pos = [];
152 b = reshape(b,0,cols);
153 if cols > 0
154 ndx = reshape(ndx,0,1);
155 end
156 return
157
158 % Handle scalar: one row.
159
160 elseif (rows == 1)
161 b = a; ndx = 1; pos = 1;
162 return
163 end
164
165 % General handling.
166 % Conversion to double not done: SORTROWS is slower for doubles
167 % than other types.
168
169 if nOut > 1
170 [b,ndx] = sortrows(a);
171 else
172 b = sortrows(a);
173 end
174
175 % d indicates the location of non-matching entries.
176
177 d = b(1:rows-1,:)~=b(2:rows,:);
178
179 % d = 1 if differences between rows. d = 0 if the rows are equal.
180
181 d = any(d,2);
182 d(rows,1) = 1; % Final row is always member of unique list.
183
184 b = b(d,:); % Create unique list by indexing into sorted list.
185
186 % Create position mapping vector using CUMSUM.
187
188 if nOut == 3
189 pos = cumsum([1;full(d)]); % Lists position, starting at 1.
190 pos(rows+1) = []; % Remove extra element introduced by d.
191 pos(ndx) = pos; % Re-reference POS to indexing of SORT.
192 end
193
194 % Create indices if needed.
195 if nOut > 1
196 ndx = ndx(d);
197 end
198 end