This is a static copy of a profile report

Home

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

Parents (calling functions)

Function NameFunction TypeCalls
build_thoughtM-function4
build_thought_2M-function4
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
149
[tf,pos] = ismember(a,b);     ...
70.016 s33.3%
32
flag = [];
80.000 s0.0%
23
nIn = nargin;
80.000 s0.0%
46
nOut = nargout;
80.000 s0.0%
44
numelA = numel(a);
80.000 s0.0%
Other lines & overhead  0.031 s66.6%
Totals  0.047 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ismemberM-function80.016 s33.3%
Self time (built-ins, overhead, etc.)  0.031 s66.7%
Totals  0.047 s100% 
M-Lint results
Line numberMessage
195The value assigned here to variable 'colsA' might never be used.
203The value assigned here to variable 'c' might never be used.
Coverage results
[ Show coverage for parent directory ]
Total lines in file243
Non-code lines (comments, blank lines)87
Code lines (lines that can run)156
Code lines that did run56
Code lines that did not run100
Coverage (did run/can run)35.90 %
Function listing
   time   calls  line
1 function [c,ia,ib] = intersect(a,b,flag)
2 %INTERSECT Set intersection.
3 % INTERSECT(A,B) when A and B are vectors returns the values common
4 % to both A and B. The result will be sorted. A and B can be cell
5 % arrays of strings.
6 %
7 % INTERSECT(A,B,'rows') when A and B are matrices with the same
8 % number of columns returns the rows common to both A and B.
9 %
10 % [C,IA,IB] = INTERSECT(...) also returns index vectors IA and IB
11 % such that C = A(IA) and C = B(IB) (or C = A(IA,:) and C = B(IB,:)).
12 %
13 % Class support for inputs A,B:
14 % float: double, single
15 %
16 % See also UNIQUE, UNION, SETDIFF, SETXOR, ISMEMBER.
17
18 % Copyright 1984-2005 The MathWorks, Inc.
19 % $Revision: 1.21.4.6 $ $Date: 2005/06/21 19:36:22 $
20
21 % Cell array implementation in @cell/intersect.m
22
< 0.01 8 23 nIn = nargin;
24
8 25 if nIn < 2
26 error('MATLAB:INTERSECT:NotEnoughInputs', 'Not enough input arguments.');
8 27 elseif nIn > 3
28 error('MATLAB:INTERSECT:TooManyInputs', 'Too many input arguments.');
29 end
30
< 0.01 8 31 if nIn == 2
< 0.01 8 32 flag = [];
< 0.01 8 33 end
34
8 35 isrows = strcmpi(flag,'rows');
36
< 0.01 8 37 rowsA = size(a,1);
< 0.01 8 38 colsA = size(a,2);
< 0.01 8 39 rowsB = size(b,1);
< 0.01 8 40 colsB = size(b,2);
41
8 42 rowvec = ~((rowsA > 1 && colsB <= 1) || (rowsB > 1 && colsA <= 1) || isrows);
43
< 0.01 8 44 numelA = numel(a);
< 0.01 8 45 numelB = numel(b);
< 0.01 8 46 nOut = nargout;
47
8 48 if isempty(flag)
49
8 50 if length(a)~=numelA || length(b)~=numelB
51 error('MATLAB:INTERSECT:AandBvectorsOrRowflag',...
52 'A and B must be vectors, or ''rows'' must be specified.');
53 end
54
8 55 c = [a([]);b([])]; % Predefined to determine class of output
56
57 % Handle empty: no elements.
58
8 59 if (numelA == 0 || numelB == 0)
60 % Predefine index outputs to be of the correct type.
61 ia = [];
62 ib = [];
63 % Ambiguous if no way to determine whether to return a row or column.
64 ambiguous = ((size(a,1)==0 && size(a,2)==0) || length(a)==1) && ...
65 ((size(b,1)==0 && size(b,2)==0) || length(b)==1);
66 if ~ambiguous
67 c = reshape(c,0,1);
68 ia = reshape(ia,0,1);
69 ib = reshape(ia,0,1);
70 end
71
72 % Handle scalars: one element.
73
8 74 elseif (numelA == 1)
75 % Scalar A: pass to ISMEMBER to determine if A exists in B.
76 [tf,pos] = ismember(a,b);
77 if tf
78 c = a;
79 ib = pos;
80 ia = 1;
81 else
82 ia = []; ib = [];
83 end
8 84 elseif (numelB == 1)
85 % Scalar B: pass to ISMEMBER to determine if B exists in A.
86 [tf,pos] = ismember(b,a);
87 if tf
88 c = b;
89 ia = pos;
90 ib = 1;
91 else
92 ia = []; ib = [];
93 end
94
95 % General handling.
96
8 97 else
98
99 % Convert to columns.
8 100 a = a(:);
8 101 b = b(:);
102
103 % Convert to double arrays, which sort faster than other types.
104
8 105 whichclass = class(c);
8 106 isdouble = strcmp(whichclass,'double');
107
8 108 if ~isdouble
109 if ~strcmp(class(a),'double')
110 a = double(a);
111 end
112 if ~strcmp(class(b),'double')
113 b = double(b);
114 end
115 end
116
117 % Switch to sort shorter list.
118
8 119 if numelA < numelB
1 120 if nOut > 1
121 [a,ia] = sort(a); % Return indices only if needed.
1 122 else
1 123 a = sort(a);
1 124 end
125
1 126 [tf,pos] = ismember(b,a); % TF lists matches at positions POS.
127
1 128 where = zeros(size(a)); % WHERE holds matching indices
1 129 where(pos(tf)) = find(pos); % from set B, 0 if unmatched.
1 130 tfs = where > 0; % TFS is logical of WHERE.
131
132 % Create intersection list.
1 133 c = a(tfs);
134
1 135 if nOut > 1
136 % Create index vectors if requested.
137 ia = ia(tfs);
138 if nOut > 2
139 ib = where(tfs);
140 end
141 end
7 142 else
7 143 if nOut > 1
144 [b,ib] = sort(b); % Return indices only if needed.
7 145 else
7 146 b = sort(b);
7 147 end
148
0.02 7 149 [tf,pos] = ismember(a,b); % TF lists matches at positions POS.
150
7 151 where = zeros(size(b)); % WHERE holds matching indices
7 152 where(pos(tf)) = find(pos); % from set B, 0 if unmatched.
7 153 tfs = where > 0; % TFS is logical of WHERE.
154
155 % Create intersection list.
7 156 c = b(tfs);
157
7 158 if nOut > 1
159 % Create index vectors if requested.
160 ia = where(tfs);
161 if nOut > 2
162 ib = ib(tfs);
163 end
164 end
7 165 end
166
167 % Re-convert to correct output data type using FEVAL.
8 168 if ~isdouble
169 c = feval(whichclass,c);
170 end
8 171 end
172
173 % If row vector, return as row vector.
8 174 if rowvec
8 175 c = c.';
8 176 if nOut > 1
177 ia = ia.';
178 if nOut > 2
179 ib = ib.';
180 end
181 end
8 182 end
183
184 else % 'rows' case
185 if ~isrows
186 error('MATLAB:INTERSECT:UnknownFlag', 'Unknown flag.');
187 end
188
189 % Automatically pad strings with spaces
190 if ischar(a) && ischar(b)
191 if colsA > colsB
192 b = [b repmat(' ',rowsB,colsA-colsB)];
193 elseif colsA < colsB
194 a = [a repmat(' ',rowsA,colsB-colsA)];
195 colsA = colsB;
196 end
197 elseif colsA ~= colsB && ~isempty(a) && ~isempty(b)
198 error('MATLAB:INTERSECT:AandBColnumAgree',...
199 'A and B must have the same number of columns.');
200 end
201
202 % Predefine the size of c, since it may return empty.
203 c = zeros(0,colsB);
204 ia = [];
205 ib = [];
206
207 % Remove duplicates from A and B. Only return indices if needed.
208 if nOut <= 1
209 a = unique(a,flag);
210 b = unique(b,flag);
211 c = sortrows([a;b]);
212 else
213 [a,ia] = unique(a,flag);
214 [b,ib] = unique(b,flag);
215 [c,ndx] = sortrows([a;b]);
216 end
217
218 % Find matching entries in sorted rows.
219 [rowsC,colsC] = size(c);
220 if rowsC > 1 && colsC ~= 0
221 % d indicates the location of matching entries
222 d = c(1:rowsC-1,:) == c(2:rowsC,:);
223 else
224 d = zeros(rowsC-1,0);
225 end
226
227 d = find(all(d,2));
228
229 c = c(d,:); % Intersect is list of matching entries
230
231 if nOut > 1
232 n = size(a,1);
233 ia = ia(ndx(d)); % IA: indices of first matches
234 ib = ib(ndx(d+1)-n); % IB: indices of second matches
235 end
236
237 % Automatically deblank strings
238 if ischar(a) && ischar(b)
239 rowsC = size(c,1);
240 c = deblank(c);
241 c = reshape(c,rowsC,size(c,2));
242 end
243 end