This is a static copy of a profile reportHome
setxor (4 calls, 0.052 sec)
Generated 18-Mar-2011 23:31:42 using cpu time.
M-function in file /Applications/MATLAB_R2010a.app/toolbox/matlab/ops/setxor.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 |
127 | tfa = ~ismembc(a,b); | 4 | 0.010 s | 20.0% |  |
49 | if isempty(flag) | 4 | 0.010 s | 20.0% |  |
47 | if ~(isa(a,'opaque') || isa(b,... | 4 | 0.010 s | 20.0% |  |
32 | end | 4 | 0.010 s | 20.0% |  |
170 | end | 4 | 0 s | 0% |  |
All other lines | | | 0.010 s | 20.0% |  |
Totals | | | 0.052 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
unique | M-function | 4 | 0 s | 0% |  |
ismembc | MEX-function | 8 | 0 s | 0% |  |
Self time (built-ins, overhead, etc.) | | | 0.052 s | 100.0% |  |
Totals | | | 0.052 s | 100% | |
M-Lint results
No M-Lint messages.Coverage results
[ Show coverage for parent directory ]
Total lines in function | 400 |
Non-code lines (comments, blank lines) | 115 |
Code lines (lines that can run) | 285 |
Code lines that did run | 47 |
Code lines that did not run | 238 |
Coverage (did run/can run) | 16.49 % |
Function listing
time calls line
1 function [c,ia,ib] = setxor(a,b,flag)
2 %SETXOR Set exclusive-or.
3 % SETXOR(A,B) when A and B are vectors returns the values that are
4 % not in the intersection of A and B. The result will be sorted.
5 % A and B can be cell arrays of strings.
6 %
7 % SETXOR(A,B,'rows') when A are B are matrices with the same number
8 % of columns returns the rows that are not in the intersection
9 % of A and B.
10 %
11 % [C,IA,IB] = SETXOR(...) also returns index vectors IA and IB such
12 % that C is a sorted combination of the elements of A(IA) and B(IB)
13 % (or A(IA,:) and B(IB,:)).
14 %
15 % See also UNIQUE, UNION, INTERSECT, SETDIFF, ISMEMBER.
16
17 % Copyright 1984-2009 The MathWorks, Inc.
18 % $Revision: 1.19.4.5 $ $Date: 2009/09/28 20:28:09 $
19
20 % Cell array implementation in @cell/setxor.m
21
4 22 nIn = nargin;
23
4 24 if nIn < 2
25 error('MATLAB:SETXOR:NotEnoughInputs', 'Not enough input arguments.');
4 26 elseif nIn > 3
27 error('MATLAB:SETXOR:TooManyInputs', 'Too many input arguments.');
28 end
29
4 30 if nIn == 2
4 31 flag = [];
0.01 4 32 end
33
4 34 isrows = strcmpi(flag,'rows');
35
4 36 rowsA = size(a,1);
4 37 colsA = size(a,2);
4 38 rowsB = size(b,1);
4 39 colsB = size(b,2);
40
4 41 rowvec = ~((rowsA > 1 && colsB <= 1) || (rowsB > 1 && colsA <= 1) || isrows);
42
4 43 numelA = numel(a);
4 44 numelB = numel(b);
4 45 nOut = nargout;
46
0.01 4 47 if ~(isa(a,'opaque') || isa(b,'opaque'))
48
0.01 4 49 if isempty(flag)
50
4 51 if length(a)~=numelA || length(b)~=numelB
52 error('MATLAB:SETXOR:AandBvectorsOrRowsFlag',...
53 'A and B must be vectors or ''rows'' must be specified.');
54 end
55
4 56 c = [a([]);b([])];
57
58 % Handle empty arrays.
59
4 60 if (numelA == 0 || numelB == 0)
61 % Predefine outputs to be of the correct type.
62 if (numelA == 0 && numelB == 0)
63 ia = []; ib = [];
64 if (max(size(a)) > 0 || max(size(b)) > 0)
65 c = reshape(c,0,1);
66 ia = reshape(ia,0,1);
67 ib = reshape(ia,0,1);
68 end
69 elseif (numelA == 0)
70 [c, ib] = unique(b(:));
71 ia = zeros(0,1);
72 else
73 [c, ia] = unique(a(:));
74 ib = zeros(0,1);
75 end
76
77 % General handling.
78
4 79 else
80
81 % Convert to columns.
4 82 a = a(:);
4 83 b = b(:);
84
85 % Convert to double arrays, which sort faster than other types.
86
4 87 whichclass = class(c);
4 88 isdouble = strcmp(whichclass,'double');
89
4 90 if ~isdouble
91 if ~strcmp(class(a),'double')
92 a = double(a);
93 end
94 if ~strcmp(class(b),'double')
95 b = double(b);
96 end
97 end
98
99 % Sort if unsorted. Only check this for long lists.
100
4 101 checksortcut = 1000;
102
4 103 if nOut <= 1
4 104 if numelA <= checksortcut || ~(issorted(a))
4 105 a = sort(a);
4 106 end
4 107 if numelB <= checksortcut || ~(issorted(b))
4 108 b = sort(b);
4 109 end
110 else
111 if numelA <= checksortcut || ~(issorted(a))
112 [a,ia] = sort(a);
113 else
114 ia = (1:numelA)';
115 end
116 if numelB <= checksortcut || ~(issorted(b))
117 [b,ib] = sort(b);
118 else
119 ib = (1:numelB)';
120 end
121 end
122
123 % Find members of the XOR set. Pass to ISMEMBC directly if
124 % possible (real, full, no NaN) since A and B are sorted double arrays.
4 125 if (isreal(a) && isreal(b)) && (~issparse(a) && ~issparse(b))
4 126 if ~isnan(b(numelB)) % Check final index of B for NaN.
0.01 4 127 tfa = ~ismembc(a,b);
128 else
129 tfa = ~ismember(a,b); % Call ISMEMBER if NaN detected in B.
130 end
4 131 if ~isnan(a(numelA)) % Check final index of A for NaN.
4 132 tfb = ~ismembc(b,a);
133 else
134 tfb = ~ismember(b,a); % Call ISMEMBER if NaN detected in A.
135 end
136 else
137 tfa = ~ismember(a,b); % If wrong types, call ISMEMBER directly.
138 tfb = ~ismember(b,a);
139 end
140
141 % a(tfa) now contains all members of A which are not in B
142 % b(tfb) now contains all members of B which are not in A
4 143 if nOut <= 1
4 144 c = unique([a(tfa);b(tfb)]); % Remove duplicates from XOR list.
145 else
146 ia = ia(tfa);
147 ib = ib(tfb);
148 n = size(ia,1);
149 [c,ndx] = unique([a(tfa);b(tfb)]); % NDX holds indices to generate C.
150 d = ndx > n; % Find indices of A and of B.
151 ia = ia(ndx(~d));
152 ib = ib(ndx(d) - n);
153 end
154
155 % Re-convert to correct output data type using FEVAL.
4 156 if ~isdouble
157 c = feval(whichclass,c);
158 end
4 159 end
160
161 % If row vector, return as row vector.
4 162 if rowvec
4 163 c = c.';
4 164 if nOut > 1
165 ia = ia.';
166 if nOut > 2
167 ib = ib.';
168 end
169 end
4 170 end
171
172 else % 'rows' case
173 if ~isrows
174 error('MATLAB:SETXOR:UnknownFlag', 'Unknown flag.');
175 end
176 % Automatically pad strings with spaces
177 if ischar(a) && ischar(b)
178 if colsA > colsB
179 b = [b repmat(' ',rowsB,colsA-colsB)];
180 elseif colsA < colsB
181 a = [a repmat(' ',rowsA,colsB-colsA)];
182 colsA = colsB;
183 end
184 elseif colsA ~= colsB
185 error('MATLAB:SETXOR:AandBColnumAgree',...
186 'A and B must have the same number of columns.');
187 end
188
189 % Handle empty arrays.
190
191 if isempty(a) && isempty(b)
192 % Predefine c to be of the correct type.
193 c = [a([]);b([])];
194 if (rowsA + rowsB == 0)
195 c = reshape(c,0,colsA);
196 elseif (colsA == 0)
197 c = reshape(c,(rowsA > 0) + (rowsB > 0),0); % Empty row array.
198 if rowsA > 0
199 ia = rowsA;
200 else
201 ia = [];
202 end
203 if rowsB > 0
204 ib = rowsB;
205 else
206 ib = [];
207 end
208 end
209
210 else
211 % Remove duplicates from A and B and sort.
212 if nOut <= 1
213 a = unique(a,flag);
214 b = unique(b,flag);
215 c = sortrows([a;b]);
216 else
217 [a,ia] = unique(a,flag);
218 [b,ib] = unique(b,flag);
219 [c,ndx] = sortrows([a;b]);
220 end
221
222 % Find all non-matching entries in sorted list.
223 [rowsC,colsC] = size(c);
224 if rowsC > 1 && colsC ~= 0
225 % d indicates the location of non-matching entries
226 d = c(1:rowsC-1,:)~=c(2:rowsC,:);
227 else
228 d = zeros(rowsC-1,0);
229 end
230
231 d = any(d,2);
232 d(rowsC,1) = 1; % Last row is always unique.
233 d(2:rowsC) = d(1:rowsC-1) & d(2:rowsC); % Remove both if match.
234
235 c = c(d,:); % Keep only the non-matching entries
236
237 if nOut > 1
238 ndx = ndx(d); % NDX: indices of non-matching entries
239 n = size(a,1);
240 d = ndx <= n; % Values in a that don't match.
241 ia = ia(ndx(d));
242 d = ndx > n; % Values in b that don't match.
243 ib = ib(ndx(d)-n);
244 end
245 end
246
247 % Automatically deblank strings
248 if ischar(a) && ischar(b)
249 c = deblank(c);
250 end
251 end
252
253 else
254 % Handle objects that cannot be converted to doubles
255 if isempty(flag)
256
257 if length(a)~=numelA || length(b)~=numelB
258 error('MATLAB:setxor:AorBinvalidSize',...
259 'A and B must be vectors or ''rows'' must be specified.');
260 end
261
262 c = [a([]);b([])];
263
264 % Handle empty arrays.
265
266 if (numelA == 0 || numelB == 0)
267 % Predefine outputs to be of the correct type.
268 if (numelA == 0 && numelB == 0)
269 ia = []; ib = [];
270 if (max(size(a)) > 0 || max(size(b)) > 0)
271 c = reshape(c,0,1);
272 ia = reshape(ia,0,1);
273 ib = reshape(ia,0,1);
274 end
275 elseif (numelA == 0)
276 [c, ib] = unique(b(:));
277 ia = zeros(0,1);
278 else
279 [c, ia] = unique(a(:));
280 ib = zeros(0,1);
281 end
282
283 % General handling.
284
285 else
286
287 % Make sure a and b contain unique elements.
288 [a,ia] = unique(a(:));
289 [b,ib] = unique(b(:));
290
291 % Find matching entries
292 e = [a;b];
293 [c,ndx] = sort(e);
294
295 % d indicates the location of matching entries
296 d = find(c(1:end-1)==c(2:end));
297 ndx([d;d+1]) = []; % Remove all matching entries
298
299 c = e(ndx);
300
301 if nOut > 1
302 n = length(a);
303 d = ndx <= n;
304 ia = ia(ndx(d)); % Find indices for set A if needed.
305 if nOut > 2
306 d = ndx > n;
307 ib = ib(ndx(d)-n); % Find indices for set B if needed.
308 end
309 end
310 end
311
312 % If row vector, return as row vector.
313 if rowvec
314 c = c.';
315 if nOut > 1
316 ia = ia.';
317 if nOut > 2
318 ib = ib.';
319 end
320 end
321 end
322
323 else % 'rows' case
324 if ~isrows
325 error('MATLAB:setxor:unknownFlag', 'Unknown flag.');
326 end
327 % Automatically pad strings with spaces
328 if ischar(a) && ischar(b)
329 if colsA > colsB
330 b = [b repmat(' ',rowsB,colsA-colsB)];
331 elseif colsA < colsB
332 a = [a repmat(' ',rowsA,colsB-colsA)];
333 colsA = colsB;
334 end
335 elseif colsA ~= colsB
336 error('MATLAB:setxor:AandBcolnumMismatch',...
337 'A and B must have the same number of columns.');
338 end
339
340 % Handle empty arrays.
341
342 if isempty(a) && isempty(b)
343 % Predefine c to be of the correct type.
344 c = [a([]);b([])];
345 ia = []; ib = [];
346 if (rowsA + rowsB == 0)
347 c = reshape(c,0,colsA);
348 elseif (colsA == 0)
349 c = reshape(c,(rowsA > 0) + (rowsB > 0),0); % Empty row array.
350 if rowsA > 0
351 ia = rowsA;
352 end
353 if rowsB > 0
354 ib = rowsB;
355 end
356 end
357
358 else
359 % Remove duplicates from A and B and sort.
360 if nOut <= 1
361 a = unique(a,flag);
362 b = unique(b,flag);
363 c = sortrows([a;b]);
364 else
365 [a,ia] = unique(a,flag);
366 [b,ib] = unique(b,flag);
367 [c,ndx] = sortrows([a;b]);
368 end
369
370 % Find all non-matching entries in sorted list.
371 [rowsC,colsC] = size(c);
372 if rowsC > 1 && colsC ~= 0
373 % d indicates the location of non-matching entries
374 d = c(1:rowsC-1,:)~=c(2:rowsC,:);
375 else
376 d = zeros(rowsC-1,0);
377 end
378
379 d = any(d,2);
380 d(rowsC,1) = 1; % Last row is always unique.
381 d(2:rowsC) = d(1:rowsC-1) & d(2:rowsC); % Remove both if match.
382
383 c = c(d,:); % Keep only the non-matching entries
384
385 if nOut > 1
386 ndx = ndx(d); % NDX: indices of non-matching entries
387 n = size(a,1);
388 d = ndx <= n; % Values in a that don't match.
389 ia = ia(ndx(d));
390 d = ndx > n; % Values in b that don't match.
391 ib = ib(ndx(d)-n);
392 end
393 end
394
395 % Automatically deblank strings
396 if ischar(a) && ischar(b)
397 c = deblank(c);
398 end
399 end
400 end