This is a static copy of a profile report

Home

setdiff (2 calls, 0.020 sec)
Generated 05-Nov-2014 07:52:38 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/matlab/ops/setdiff.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
close>safegetchildrensubfunction2
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
45
if isempty(flag)
20.010 s50.0%
30
end
20.010 s50.0%
74
return
20 s0%
70
c = unique(a);
20 s0%
69
if nOut <= 1
20 s0%
All other lines  0 s0%
Totals  0.020 s100% 
Children (called functions)

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