This is a static copy of a profile report

Home

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

Parents (calling functions)

Function NameFunction TypeCalls
graphics/private/clofunction496
linkaxesfunction1
scribe.legend.methods>lscansubfunction9
close>safegetchildrensubfunction34
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
112
tf = ~(ismember(a,b));
1000.055 s26.3%
117
c = unique(c);
1000.022 s10.5%
81
if ~ismember(a,b,flag)
1530.022 s10.5%
45
if isempty(flag)
5400.022 s10.5%
41
nOut = nargout;
5400.022 s10.5%
All other lines  0.066 s31.6%
Totals  0.208 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ismemberfunction2530.077 s36.8%
uniquefunction2320.033 s15.8%
Self time (built-ins, overhead, etc.)  0.098 s47.4%
Totals  0.208 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in function338
Non-code lines (comments, blank lines)124
Code lines (lines that can run)214
Code lines that did run55
Code lines that did not run159
Coverage (did run/can run)25.70 %
Function listing
   time   calls  line
1 function [c,ia] = setdiff(a,b,flag)
2 %SETDIFF Set difference.
3 % SETDIFF(A,B) when A and B are vectors returns the values
4 % in A that are not in B. The result will be sorted. A and B
5 % can be cell arrays of strings.
6 %
7 % SETDIFF(A,B,'rows') when A are B are matrices with the same
8 % number of columns returns the rows from A that are not in B.
9 %
10 % [C,I] = SETDIFF(...) also returns an index vector I such that
11 % C = A(I) (or C = A(I,:)).
12 %
13 % See also UNIQUE, UNION, INTERSECT, SETXOR, ISMEMBER.
14
15 % Copyright 1984-2009 The MathWorks, Inc.
16 % $Revision: 1.22.4.5 $ $Date: 2009/09/28 20:28:08 $
17
18 % Cell array implementation in @cell/setdiff.m
19
540 20 nIn = nargin;
21
540 22 if nIn < 2
23 error('MATLAB:SETDIFF:NotEnoughInputs', 'Not enough input arguments.');
540 24 elseif nIn > 3
25 error('MATLAB:SETDIFF:TooManyInputs', 'Too many input arguments.');
26 end
27
540 28 if nIn == 2
0.02 540 29 flag = [];
0.02 540 30 end
31
540 32 isrows = strcmpi(flag,'rows');
33
540 34 rowsA = size(a,1);
540 35 colsA = size(a,2);
540 36 rowsB = size(b,1);
540 37 colsB = size(b,2);
38
540 39 rowvec = ~((rowsA > 1 && colsB <= 1) || (rowsB > 1 && colsA <= 1) || isrows);
40
0.02 540 41 nOut = nargout;
42
540 43 if ~(isa(a,'opaque') || isa(b,'opaque'))
44
0.02 540 45 if isempty(flag)
46
540 47 numelA = length(a);
540 48 numelB = length(b);
49
540 50 if numel(a)~=numelA || numel(b)~=numelB
51 error('MATLAB:SETDIFF:AandBvectorsOrRowsFlag', ...
52 'A and B must be vectors or ''rows'' must be specified.');
53 end
54
55 % Handle empty arrays.
56
540 57 if (numelA == 0)
58 % Predefine outputs to be of the correct type.
155 59 c = a([]);
155 60 ia = [];
61 % Ambiguous if no way to determine whether to return a row or column.
155 62 ambiguous = (rowsA==0 && colsA==0) && ...
63 ((rowsB==0 && colsB==0) || numelB == 1);
155 64 if ~ambiguous
2 65 c = reshape(c,0,1);
2 66 ia = reshape(ia,0,1);
2 67 end
385 68 elseif (numelB == 0)
69 % If B is empty, invoke UNIQUE to remove duplicates from A.
132 70 if nOut <= 1
0.01 132 71 c = unique(a);
72 else
73 [c,ia] = unique(a);
74 end
132 75 return
76
77 % Handle scalar: one element. Scalar A done only.
78 % Scalar B handled within ISMEMBER and general implementation.
79
253 80 elseif (numelA == 1)
0.02 153 81 if ~ismember(a,b,flag)
82 c = a;
83 ia = 1;
153 84 else
153 85 c = [];
153 86 ia = [];
153 87 end
153 88 return
89
90 % General handling.
91
100 92 else
93
94 % Convert to columns.
100 95 a = a(:);
100 96 b = b(:);
97
98 % Convert to double arrays, which sort faster than other types.
99
100 100 whichclass = class(a);
100 101 isdouble = strcmp(whichclass,'double');
102
100 103 if ~isdouble
104 a = double(a);
105 end
106
100 107 if ~strcmp(class(b),'double')
108 b = double(b);
109 end
110
111 % Call ISMEMBER to determine list of non-matching elements of A.
0.05 100 112 tf = ~(ismember(a,b));
100 113 c = a(tf);
114
115 % Call UNIQUE to remove duplicates from list of non-matches.
100 116 if nargout <= 1
0.02 100 117 c = unique(c);
118 else
119 [c,ndx] = unique(c);
120
121 % Find indices by using TF and NDX.
122 where = find(tf);
123 ia = where(ndx);
124 end
125
126 % Re-convert to correct output data type using FEVAL.
100 127 if ~isdouble
128 c = feval(whichclass,c);
129 end
100 130 end
131
132 % If row vector, return as row vector.
255 133 if rowvec
156 134 c = c.';
156 135 if nOut > 1
136 ia = ia.';
137 end
0.01 156 138 end
139
140 else % 'rows' case
141 if ~isrows
142 error('MATLAB:SETDIFF:UnknownFlag', 'Unknown flag.');
143 end
144
145 % Automatically pad strings with spaces
146 if ischar(a) && ischar(b)
147 if colsA > colsB
148 b = [b repmat(' ',rowsB,colsA-colsB)];
149 elseif colsA < colsB
150 a = [a repmat(' ',rowsA,colsB-colsA)];
151 colsA = colsB;
152 end
153 elseif colsA ~= colsB
154 error('MATLAB:SETDIFF:AandBColnumAgree',...
155 'A and B must have the same number of columns.');
156 end
157
158 % Handle empty arrays
159 if rowsA == 0
160 c = zeros(rowsA,colsA);
161 ia = [];
162 elseif colsA == 0 && rowsA > 0
163 c = zeros(1,0);
164 ia = rowsA;
165 % General handling
166 else
167 % Remove duplicates from A; get indices only if needed
168 if nOut > 1
169 [a,ia] = unique(a,flag);
170 else
171 a = unique(a,flag);
172 end
173
174 % Create sorted list of unique A and B; want non-matching entries
175 [c,ndx] = sortrows([a;b]);
176 [rowsC,colsC] = size(c);
177 if rowsC > 1 && colsC ~= 0
178 % d indicates the location of non-matching entries
179 d = c(1:rowsC-1,:) ~= c(2:rowsC,:);
180 else
181 d = zeros(rowsC-1,0);
182 end
183 d = any(d,2);
184 d(rowsC,1) = 1; % Final entry always included.
185
186 % d = 1 now for any unmatched entry of A or of B.
187 n = size(a,1);
188 d = d & ndx <= n; % Now find only the ones in A.
189
190 c = c(d,:);
191
192 if nOut > 1
193 ia = ia(ndx(d));
194 end
195 end
196 end
197
198 % Automatically deblank strings
255 199 if ischar(a)
200 c = deblank(c);
201 end
202
203 else
204 % Handle objects that cannot be converted to doubles
205 if isempty(flag)
206
207 numelA = length(a);
208 numelB = length(b);
209
210 if numel(a)~=numelA || numel(b)~=numelB
211 error('MATLAB:setdiff:AorBinvalidSize',...
212 'A and B must be vectors or ''rows'' must be specified.');
213 end
214
215 % Handle empty arrays.
216
217 if (numelA == 0)
218 % Predefine outputs to be of the correct type.
219 c = a([]);
220 ia = [];
221 % Ambiguous if no way to determine whether to return a row or column.
222 ambiguous = (rowsA==0 && colsA==0) && ...
223 ((rowsB==0 && colsB==0) || numelB == 1);
224 if ~ambiguous
225 c = reshape(c,0,1);
226 ia = reshape(ia,0,1);
227 end
228 elseif (numelB == 0)
229 % If B is empty, invoke UNIQUE to remove duplicates from A.
230 if nOut <= 1
231 c = unique(a);
232 else
233 [c,ia] = unique(a);
234 end
235 return
236
237 % General handling.
238
239 else
240
241 % Make sure a and b contain unique elements.
242 if nOut > 1
243 [a,ia] = unique(a(:));
244 else
245 a = unique(a(:));
246 end
247
248 b = unique(b(:));
249
250 % Find matching entries
251 [c,ndx] = sort([a;b]);
252
253 % d indicates the location of matching entries
254 d = find(c(1:end-1)==c(2:end));
255
256 % Remove all matching entries
257 ndx([d;d+1]) = [];
258
259 d = ndx <= length(a); % Values in a that don't match.
260
261 c = a(ndx(d));
262
263 if nOut > 1
264 ia = ia(ndx(d));
265 end
266 end
267
268 % If row vector, return as row vector.
269 if rowvec
270 c = c.';
271 if nOut > 1
272 ia = ia.';
273 end
274 end
275
276 else % 'rows' case
277 if ~isrows
278 error('MATLAB:setdiff:UnknownFlag', 'Unknown flag.');
279 end
280
281 % Automatically pad strings with spaces
282 if ischar(a) && ischar(b)
283 if colsA > colsB
284 b = [b repmat(' ',rowsB,colsA-colsB)];
285 elseif colsA < colsB
286 a = [a repmat(' ',rowsA,colsB-colsA)];
287 colsA = colsB;
288 end
289 elseif colsA ~= colsB
290 error('MATLAB:setdiff:AandBcolnumMismatch',...
291 'A and B must have the same number of columns.');
292 end
293
294 % Handle empty arrays
295 if rowsA == 0
296 c = zeros(rowsA,colsA);
297 ia = [];
298 elseif colsA == 0 && rowsA > 0
299 c = zeros(1,0);
300 ia = rowsA;
301 % General handling
302 else
303 % Remove duplicates from A; get indices only if needed
304 if nOut > 1
305 [a,ia] = unique(a,flag);
306 else
307 a = unique(a,flag);
308 end
309
310 % Create sorted list of unique A and B; want non-matching entries
311 [c,ndx] = sortrows([a;b]);
312 [rowsC,colsC] = size(c);
313 if rowsC > 1 && colsC ~= 0
314 % d indicates the location of non-matching entries
315 d = c(1:rowsC-1,:) ~= c(2:rowsC,:);
316 else
317 d = zeros(rowsC-1,0);
318 end
319 d = any(d,2);
320 d(rowsC,1) = 1; % Final entry always included.
321
322 % d = 1 now for any unmatched entry of A or of B.
323 n = size(a,1);
324 d = d & ndx <= n; % Now find only the ones in A.
325
326 c = c(d,:);
327
328 if nOut > 1
329 ia = ia(ndx(d));
330 end
331 end
332 end
333
334 % Automatically deblank strings
335 if ischar(a)
336 c = deblank(c);
337 end
338 end