This is a static copy of a profile report

Home

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

Parents (calling functions)

Function NameFunction TypeCalls
close>request_close_helpersubfunction1
tformarray>fullsizessubfunction3442
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
76
tf(i) = any(a(i)==s(:));   % A...
103260.130 s23.2%
77
end
103260.090 s16.1%
41
if ~(isa(a,'opaque') || isa(s,...
34430.040 s7.1%
72
scalarcut = 5;
34420.030 s5.4%
55
if numelA == 0 || numelS <=...
34430.030 s5.4%
All other lines  0.240 s42.9%
Totals  0.561 s100% 
Children (called functions)
No children
Code Analyzer results
No Code Analyzer messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in function371
Non-code lines (comments, blank lines)111
Code lines (lines that can run)260
Code lines that did run24
Code lines that did not run236
Coverage (did run/can run)9.23 %
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.9 $ $Date: 2011/04/16 06:39:33 $
22
23 % Cell array implementation in @cell/ismember.m
24
0.02 3443 25 nIn = nargin;
26
3443 27 if nIn < 2
28 error(message('MATLAB:ISMEMBER:NotEnoughInputs'));
3443 29 elseif nIn > 3
30 error(message('MATLAB:ISMEMBER:TooManyInputs'));
31 end
32
0.01 3443 33 if nIn == 2
3443 34 flag = [];
0.01 3443 35 end
36
3443 37 numelA = numel(a);
0.03 3443 38 numelS = numel(s);
0.01 3443 39 nOut = nargout;
40
0.04 3443 41 if ~(isa(a,'opaque') || isa(s,'opaque'))
42
0.03 3443 43 if isempty(flag)
44
45 % Initialize types and sizes.
46
0.01 3443 47 tf = false(size(a));
48
0.02 3443 49 if nOut > 1
50 loc = zeros(size(a));
51 end
52
53 % Handle empty arrays and scalars.
54
0.03 3443 55 if numelA == 0 || numelS <= 1
1 56 if (numelA == 0 || numelS == 0)
1 57 return
58
59 % Scalar A handled below.
60 % Scalar S: find which elements of A are equal to S.
61 elseif numelS == 1
62 tf = (a == s);
63 if nOut > 1
64 % Use DOUBLE to convert logical "1" index to double "1" index.
65 loc = double(tf);
66 end
67 return
68 end
0.01 3442 69 else
70 % General handling.
71 % Use FIND method for very small sizes of the input vector to avoid SORT.
0.03 3442 72 scalarcut = 5;
0.01 3442 73 if numelA <= scalarcut
0.01 3442 74 if nOut <= 1
0.02 3442 75 for i=1:numelA
0.13 10326 76 tf(i) = any(a(i)==s(:)); % ANY returns logical.
0.09 10326 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
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.
90 a = double(a);
91 if issparse(a)
92 a = full(a);
93 end
94
95 s = double(s);
96 if issparse(s)
97 s = full(s);
98 end
99
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
104 checksortcut = 1000;
105 if numelS > checksortcut
106 sortedlist = issorted(s(:));
107 else
108 sortedlist = 0;
109 end
110 if nOut > 1
111 if ~sortedlist
112 [s,idx] = sort(s(:));
113 end
114 elseif ~sortedlist
115 s = sort(s(:));
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
134 nana = isnan(a(:));
135
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
149 else
150 % No NaN values, call ISMEMBC directly.
151 if nOut <= 1
152 tf = ismembc(a,s);
153 else
154 loc = ismembc2(a,s);
155 tf = (loc > 0);
156 end
157 end
158
159 if nOut > 1 && ~sortedlist
160 % Re-reference loc to original list if it was unsorted
161 loc(tf) = idx(loc(tf));
162 end
163 end
3442 164 end
165
166 else % 'rows' case
167 if ~strcmpi(flag,'rows')
168 error(message('MATLAB:ISMEMBER:UnknownFlag'));
169 end
170
171 rowsA = size(a,1);
172 colsA = size(a,2);
173 rowsS = size(s,1);
174 colsS = size(s,2);
175
176 % Automatically pad strings with spaces
177 if ischar(a) && ischar(s),
178 if colsA > colsS
179 s = [s repmat(' ',rowsS,colsA-colsS)];
180 elseif colsA < colsS
181 a = [a repmat(' ',rowsA,colsS-colsA)];
182 end
183 elseif colsA ~= colsS && ~isempty(a) && ~isempty(s)
184 error(message('MATLAB:ISMEMBER:AandBColnumAgree'));
185 end
186
187 % Empty check for 'rows'.
188 if rowsA == 0 || rowsS == 0
189 if (isempty(a) || isempty(s))
190 tf = false(rowsA,1);
191 loc = zeros(rowsA,1);
192 return
193 end
194 end
195
196 % General handling for 'rows'.
197
198 % Duplicates within the sets are eliminated
199 if (rowsA == 1)
200 au = repmat(a,rowsS,1);
201 d = au(1:end,:)==s(1:end,:);
202 d = all(d,2);
203 tf = any(d);
204 if nOut > 1
205 if tf
206 loc = find(d, 1, 'last');
207 else
208 loc = 0;
209 end
210 end
211 return;
212 else
213 [au,~,an] = unique(a,'rows');
214 end
215 if nOut <= 1
216 su = unique(s,'rows');
217 else
218 [su,sm] = unique(s,'rows');
219 end
220
221 % Sort the unique elements of A and S, duplicate entries are adjacent
222 [c,ndx] = sortrows([au;su]);
223
224 % Find matching entries
225 d = c(1:end-1,:)==c(2:end,:); % d indicates matching entries in 2-D
226 d = find(all(d,2)); % Finds the index of matching entries
227 ndx1 = ndx(d); % NDX1 are locations of repeats in C
228
229 if nOut <= 1
230 tf = ismember(an,ndx1); % Find repeats among original list
231 else
232 szau = size(au,1);
233 [tf,loc] = ismember(an,ndx1); % Find loc by using given indices
234 newd = d(loc(tf)); % NEWD is D for non-unique A
235 where = sm(ndx(newd+1)-szau); % Index values of SU through UNIQUE
236 loc(tf) = where; % Return last occurrence of A within S
237 end
238 end
239 else
240 % Handle objects that cannot be converted to doubles
241 if isempty(flag)
242
243 % Handle empty arrays and scalars.
244
245 if numelA == 0 || numelS <= 1
246 if (numelA == 0 || numelS == 0)
247 tf = false(size(a));
248 loc = zeros(size(a));
249 return
250
251 % Scalar A handled below.
252 % Scalar S: find which elements of A are equal to S.
253 elseif numelS == 1
254 tf = (a == s);
255 if nOut > 1
256 % Use DOUBLE to convert logical "1" index to double "1" index.
257 loc = double(tf);
258 end
259 return
260 end
261 else
262 % General handling.
263 % Use FIND method for very small sizes of the input vector to avoid SORT.
264 scalarcut = 5;
265 if numelA <= scalarcut
266 tf = false(size(a));
267 loc = zeros(size(a));
268 if nOut <= 1
269 for i=1:numelA
270 tf(i) = any(a(i)==s); % ANY returns logical.
271 end
272 else
273 for i=1:numelA
274 found = find(a(i)==s); % FIND returns indices for LOC.
275 if ~isempty(found)
276 tf(i) = 1;
277 loc(i) = found(end);
278 end
279 end
280 end
281 else
282
283 % Duplicates within the sets are eliminated
284 [au,~,an] = unique(a(:));
285 if nOut <= 1
286 su = unique(s(:));
287 else
288 [su,sm] = unique(s(:));
289 end
290
291 % Sort the unique elements of A and S, duplicate entries are adjacent
292 [c,ndx] = sort([au;su]);
293
294 % Find matching entries
295 d = c(1:end-1)==c(2:end); % d indicates matching entries in 2-D
296 d = find(d); % Finds the index of matching entries
297 ndx1 = ndx(d); % NDX1 are locations of repeats in C
298
299 if nOut <= 1
300 tf = ismember(an,ndx1); % Find repeats among original list
301 else
302 szau = size(au,1);
303 [tf,loc] = ismember(an,ndx1); % Find loc by using given indices
304 newd = d(loc(tf)); % NEWD is D for non-unique A
305 where = sm(ndx(newd+1)-szau); % Index values of SU through UNIQUE
306 loc(tf) = where; % Return last occurrence of A within S
307 end
308 end
309 tf = reshape(tf,size(a));
310 if nOut > 1
311 loc = reshape(loc,size(a));
312 end
313 end
314
315 else % 'rows' case
316 if ~strcmpi(flag,'rows')
317 error(message('MATLAB:ISMEMBER:UnknownFlag'));
318 end
319
320 rowsA = size(a,1);
321 colsA = size(a,2);
322 rowsS = size(s,1);
323 colsS = size(s,2);
324
325 % Automatically pad strings with spaces
326 if ischar(a) && ischar(s),
327 if colsA > colsS
328 s = [s repmat(' ',rowsS,colsA-colsS)];
329 elseif colsA < colsS
330 a = [a repmat(' ',rowsA,colsS-colsA)];
331 end
332 elseif size(a,2)~=size(s,2) && ~isempty(a) && ~isempty(s)
333 error(message('MATLAB:ISMEMBER:AandBColnumAgree'));
334 end
335
336 % Empty check for 'rows'.
337 if rowsA == 0 || rowsS == 0
338 if (isempty(a) || isempty(s))
339 tf = false(rowsA,1);
340 loc = zeros(rowsA,1);
341 return
342 end
343 end
344
345 % Duplicates within the sets are eliminated
346 [au,~,an] = unique(a,'rows');
347 if nOut <= 1
348 su = unique(s,'rows');
349 else
350 [su,sm] = unique(s,'rows');
351 end
352
353 % Sort the unique elements of A and S, duplicate entries are adjacent
354 [c,ndx] = sortrows([au;su]);
355
356 % Find matching entries
357 d = c(1:end-1,:)==c(2:end,:); % d indicates matching entries in 2-D
358 d = find(all(d,2)); % Finds the index of matching entries
359 ndx1 = ndx(d); % NDX1 are locations of repeats in C
360
361 if nOut <= 1
362 tf = ismember(an,ndx1); % Find repeats among original list
363 else
364 szau = size(au,1);
365 [tf,loc] = ismember(an,ndx1); % Find loc by using given indices
366 newd = d(loc(tf)); % NEWD is D for non-unique A
367 where = sm(ndx(newd+1)-szau); % Index values of SU through UNIQUE
368 loc(tf) = where; % Return last occurrence of A within S
369 end
370 end
371 end