This is a static copy of a profile report

Home

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

Parents (calling functions)

Function NameFunction TypeCalls
imagesci/private/writepngfunction120
cell.ismemberfunction40
setdifffunction253
close>request_close_helpersubfunction17
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
39
nOut = nargout;
4300.022 s15.4%
182
end
400.011 s7.7%
77
end
1560.011 s7.7%
73
if numelA <= scalarcut
1300.011 s7.7%
55
if numelA == 0 || numelS <=...
3900.011 s7.7%
All other lines  0.077 s53.8%
Totals  0.142 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ismembcMEX-file10 s0%
repmatfunction800 s0%
Self time (built-ins, overhead, etc.)  0.142 s100.0%
Totals  0.142 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in function373
Non-code lines (comments, blank lines)113
Code lines (lines that can run)260
Code lines that did run70
Code lines that did not run190
Coverage (did run/can run)26.92 %
Function listing
   time   calls  line
1 function [tf,loc] = ismember(a,s,flag)
2 %ISMEMBER True for set member.
3 % ISMEMBER(A,S) for the array A returns an array of the same size as A
4 % containing 1 where the elements of A are in the set S and 0 otherwise.
5 % A and S can be cell arrays of strings.
6 %
7 % ISMEMBER(A,S,'rows') when A and S are matrices with the same
8 % number of columns returns a vector containing 1 where the rows of
9 % A are also rows of S and 0 otherwise.
10 %
11 % [TF,LOC] = ISMEMBER(...) also returns an array LOC containing the
12 % highest absolute index in S for each element in A which is a member of S
13 % and 0 if there is no such index.
14 %
15 % Class support for inputs A,S:
16 % float: double, single
17 %
18 % See also UNIQUE, INTERSECT, SETDIFF, SETXOR, UNION.
19
20 % Copyright 1984-2009 The MathWorks, Inc.
21 % $Revision: 1.23.4.8 $ $Date: 2009/09/28 20:28:07 $
22
23 % Cell array implementation in @cell/ismember.m
24
430 25 nIn = nargin;
26
430 27 if nIn < 2
28 error('MATLAB:ISMEMBER:NotEnoughInputs', 'Not enough input arguments.');
430 29 elseif nIn > 3
30 error('MATLAB:ISMEMBER:TooManyInputs', 'Too many input arguments.');
31 end
32
0.01 430 33 if nIn == 2
237 34 flag = [];
237 35 end
36
430 37 numelA = numel(a);
430 38 numelS = numel(s);
0.02 430 39 nOut = nargout;
40
0.01 430 41 if ~(isa(a,'opaque') || isa(s,'opaque'))
42
0.01 430 43 if isempty(flag)
44
45 % Initialize types and sizes.
46
390 47 tf = false(size(a));
48
390 49 if nOut > 1
50 loc = zeros(size(a));
51 end
52
53 % Handle empty arrays and scalars.
54
0.01 390 55 if numelA == 0 || numelS <= 1
260 56 if (numelA == 0 || numelS == 0)
17 57 return
58
59 % Scalar A handled below.
60 % Scalar S: find which elements of A are equal to S.
243 61 elseif numelS == 1
243 62 tf = (a == s);
243 63 if nOut > 1
64 % Use DOUBLE to convert logical "1" index to double "1" index.
65 loc = double(tf);
66 end
243 67 return
68 end
130 69 else
70 % General handling.
71 % Use FIND method for very small sizes of the input vector to avoid SORT.
130 72 scalarcut = 5;
0.01 130 73 if numelA <= scalarcut
129 74 if nOut <= 1
129 75 for i=1:numelA
156 76 tf(i) = any(a(i)==s(:)); % ANY returns logical.
0.01 156 77 end
78 else
79 for i=1:numelA
80 found = find(a(i)==s(:)); % FIND returns indices for LOC.
81 if ~isempty(found)
82 tf(i) = 1;
83 loc(i) = found(end);
84 end
85 end
86 end
1 87 else
88 % Use method which sorts list, then performs binary search.
89 % Convert to double for quicker sorting, to full to work in C helper.
1 90 a = double(a);
1 91 if issparse(a)
92 a = full(a);
93 end
94
1 95 s = double(s);
1 96 if issparse(s)
97 s = full(s);
98 end
99
1 100 if (isreal(s))
101 % Find out whether list is presorted before sort
102 % If the list is short enough, SORT will be faster than ISSORTED
103 % If the list is longer, ISSORTED can potentially save time
1 104 checksortcut = 1000;
1 105 if numelS > checksortcut
106 sortedlist = issorted(s(:));
1 107 else
1 108 sortedlist = 0;
1 109 end
1 110 if nOut > 1
111 if ~sortedlist
112 [s,idx] = sort(s(:));
113 end
1 114 elseif ~sortedlist
1 115 s = sort(s(:));
1 116 end
117 else
118 sortedlist = 0;
119 [~,idx] = sort(real(s(:)));
120 s = s(idx);
121 end
122
123 % Two C-Helper Functions are used in the code below:
124
125 % ISMEMBC - S must be sorted - Returns logical vector indicating which
126 % elements of A occur in S
127 % ISMEMBC2 - S must be sorted - Returns a vector of the locations of
128 % the elements of A occurring in S. If multiple instances occur,
129 % the last occurrence is returned
130
131 % Check for NaN values - NaN values will be at the end of S,
132 % but may be anywhere in A.
133
1 134 nana = isnan(a(:));
135
1 136 if (any(nana) || isnan(s(numelS)))
137 % If NaNs detected, remove NaNs from the data before calling ISMEMBC.
138 ida = (nana == 0);
139 ids = (isnan(s(:)) == 0);
140 if nOut <= 1
141 ainfn = ismembc(a(ida),s(ids));
142 tf(ida) = ainfn;
143 else
144 loc1 = ismembc2(a(ida),s(ids));
145 tf(ida) = (loc1 > 0);
146 loc(ida) = loc1;
147 loc(~ida) = 0;
148 end
1 149 else
150 % No NaN values, call ISMEMBC directly.
1 151 if nOut <= 1
1 152 tf = ismembc(a,s);
153 else
154 loc = ismembc2(a,s);
155 tf = (loc > 0);
156 end
1 157 end
158
1 159 if nOut > 1 && ~sortedlist
160 % Re-reference loc to original list if it was unsorted
161 loc(tf) = idx(loc(tf));
162 end
1 163 end
130 164 end
165
40 166 else % 'rows' case
40 167 if ~strcmpi(flag,'rows')
168 error('MATLAB:ISMEMBER:UnknownFlag', 'Unknown flag.');
169 end
170
40 171 rowsA = size(a,1);
40 172 colsA = size(a,2);
40 173 rowsS = size(s,1);
40 174 colsS = size(s,2);
175
176 % Automatically pad strings with spaces
40 177 if ischar(a) && ischar(s),
40 178 if colsA > colsS
179 s = [s repmat(' ',rowsS,colsA-colsS)];
40 180 elseif colsA < colsS
40 181 a = [a repmat(' ',rowsA,colsS-colsA)];
0.01 40 182 end
183 elseif colsA ~= colsS && ~isempty(a) && ~isempty(s)
184 error('MATLAB:ISMEMBER:AandBColnumAgree', ...
185 'A and S must have the same number of columns.');
186 end
187
188 % Empty check for 'rows'.
40 189 if rowsA == 0 || rowsS == 0
190 if (isempty(a) || isempty(s))
191 tf = false(rowsA,1);
192 loc = zeros(rowsA,1);
193 return
194 end
195 end
196
197 % General handling for 'rows'.
198
199 % Duplicates within the sets are eliminated
40 200 if (rowsA == 1)
40 201 au = repmat(a,rowsS,1);
40 202 d = au(1:end,:)==s(1:end,:);
40 203 d = all(d,2);
40 204 tf = any(d);
40 205 if nOut > 1
206 if tf
207 loc = find(d, 1, 'last');
208 else
209 loc = 0;
210 end
211 end
40 212 return;
213 else
214 [au,~,an] = unique(a,'rows');
215 end
216 if nOut <= 1
217 su = unique(s,'rows');
218 else
219 [su,sm] = unique(s,'rows');
220 end
221
222 % Sort the unique elements of A and S, duplicate entries are adjacent
223 [c,ndx] = sortrows([au;su]);
224
225 % Find matching entries
226 d = c(1:end-1,:)==c(2:end,:); % d indicates matching entries in 2-D
227 d = find(all(d,2)); % Finds the index of matching entries
228 ndx1 = ndx(d); % NDX1 are locations of repeats in C
229
230 if nOut <= 1
231 tf = ismember(an,ndx1); % Find repeats among original list
232 else
233 szau = size(au,1);
234 [tf,loc] = ismember(an,ndx1); % Find loc by using given indices
235 newd = d(loc(tf)); % NEWD is D for non-unique A
236 where = sm(ndx(newd+1)-szau); % Index values of SU through UNIQUE
237 loc(tf) = where; % Return last occurrence of A within S
238 end
239 end
240 else
241 % Handle objects that cannot be converted to doubles
242 if isempty(flag)
243
244 % Handle empty arrays and scalars.
245
246 if numelA == 0 || numelS <= 1
247 if (numelA == 0 || numelS == 0)
248 tf = false(size(a));
249 loc = zeros(size(a));
250 return
251
252 % Scalar A handled below.
253 % Scalar S: find which elements of A are equal to S.
254 elseif numelS == 1
255 tf = (a == s);
256 if nOut > 1
257 % Use DOUBLE to convert logical "1" index to double "1" index.
258 loc = double(tf);
259 end
260 return
261 end
262 else
263 % General handling.
264 % Use FIND method for very small sizes of the input vector to avoid SORT.
265 scalarcut = 5;
266 if numelA <= scalarcut
267 tf = false(size(a));
268 loc = zeros(size(a));
269 if nOut <= 1
270 for i=1:numelA
271 tf(i) = any(a(i)==s); % ANY returns logical.
272 end
273 else
274 for i=1:numelA
275 found = find(a(i)==s); % FIND returns indices for LOC.
276 if ~isempty(found)
277 tf(i) = 1;
278 loc(i) = found(end);
279 end
280 end
281 end
282 else
283
284 % Duplicates within the sets are eliminated
285 [au,~,an] = unique(a(:));
286 if nOut <= 1
287 su = unique(s(:));
288 else
289 [su,sm] = unique(s(:));
290 end
291
292 % Sort the unique elements of A and S, duplicate entries are adjacent
293 [c,ndx] = sort([au;su]);
294
295 % Find matching entries
296 d = c(1:end-1)==c(2:end); % d indicates matching entries in 2-D
297 d = find(d); % Finds the index of matching entries
298 ndx1 = ndx(d); % NDX1 are locations of repeats in C
299
300 if nOut <= 1
301 tf = ismember(an,ndx1); % Find repeats among original list
302 else
303 szau = size(au,1);
304 [tf,loc] = ismember(an,ndx1); % Find loc by using given indices
305 newd = d(loc(tf)); % NEWD is D for non-unique A
306 where = sm(ndx(newd+1)-szau); % Index values of SU through UNIQUE
307 loc(tf) = where; % Return last occurrence of A within S
308 end
309 end
310 tf = reshape(tf,size(a));
311 if nOut > 1
312 loc = reshape(loc,size(a));
313 end
314 end
315
316 else % 'rows' case
317 if ~strcmpi(flag,'rows')
318 error('MATLAB:ismember:UnknownFlag', 'Unknown flag.');
319 end
320
321 rowsA = size(a,1);
322 colsA = size(a,2);
323 rowsS = size(s,1);
324 colsS = size(s,2);
325
326 % Automatically pad strings with spaces
327 if ischar(a) && ischar(s),
328 if colsA > colsS
329 s = [s repmat(' ',rowsS,colsA-colsS)];
330 elseif colsA < colsS
331 a = [a repmat(' ',rowsA,colsS-colsA)];
332 end
333 elseif size(a,2)~=size(s,2) && ~isempty(a) && ~isempty(s)
334 error('MATLAB:ismember:AandBcolnumMismatch',...
335 'A and S must have the same number of columns.');
336 end
337
338 % Empty check for 'rows'.
339 if rowsA == 0 || rowsS == 0
340 if (isempty(a) || isempty(s))
341 tf = false(rowsA,1);
342 loc = zeros(rowsA,1);
343 return
344 end
345 end
346
347 % Duplicates within the sets are eliminated
348 [au,~,an] = unique(a,'rows');
349 if nOut <= 1
350 su = unique(s,'rows');
351 else
352 [su,sm] = unique(s,'rows');
353 end
354
355 % Sort the unique elements of A and S, duplicate entries are adjacent
356 [c,ndx] = sortrows([au;su]);
357
358 % Find matching entries
359 d = c(1:end-1,:)==c(2:end,:); % d indicates matching entries in 2-D
360 d = find(all(d,2)); % Finds the index of matching entries
361 ndx1 = ndx(d); % NDX1 are locations of repeats in C
362
363 if nOut <= 1
364 tf = ismember(an,ndx1); % Find repeats among original list
365 else
366 szau = size(au,1);
367 [tf,loc] = ismember(an,ndx1); % Find loc by using given indices
368 newd = d(loc(tf)); % NEWD is D for non-unique A
369 where = sm(ndx(newd+1)-szau); % Index values of SU through UNIQUE
370 loc(tf) = where; % Return last occurrence of A within S
371 end
372 end
373 end