time | Calls | line |
---|
| | 268 | function [lia,locb] = ismemberBuiltinTypes(a,b)
|
| | 269 | % General handling.
|
| | 270 | % Use FIND method for very small sizes of the input vector to avoid SORT.
|
< 0.001 | 19 | 271 | if nargout > 1
|
< 0.001 | 4 | 272 | locb = zeros(size(a));
|
< 0.001 | 4 | 273 | end
|
| | 274 | % Handle empty arrays and scalars.
|
< 0.001 | 19 | 275 | numelA = numel(a);
|
< 0.001 | 19 | 276 | numelB = numel(b);
|
< 0.001 | 19 | 277 | if numelA == 0 || numelB <= 1
|
< 0.001 | 14 | 278 | if numelA > 0 && numelB == 1
|
| | 279 | lia = (a == b);
|
| | 280 | if nargout > 1
|
| | 281 | % Use DOUBLE to convert logical "1" index to double "1" index.
|
| | 282 | locb = double(lia);
|
| | 283 | end
|
< 0.001 | 14 | 284 | else
|
< 0.001 | 14 | 285 | lia = false(size(a));
|
< 0.001 | 14 | 286 | end
|
< 0.001 | 14 | 287 | return
|
| | 288 | end
|
| | 289 |
|
< 0.001 | 5 | 290 | scalarcut = 5;
|
< 0.001 | 5 | 291 | if numelA <= scalarcut
|
< 0.001 | 5 | 292 | lia = false(size(a));
|
< 0.001 | 5 | 293 | if nargout <= 1
|
< 0.001 | 5 | 294 | for i=1:numelA
|
< 0.001 | 10 | 295 | lia(i) = any(a(i)==b(:));
|
< 0.001 | 10 | 296 | end
|
| | 297 | else
|
| | 298 | for i=1:numelA
|
| | 299 | found = a(i)==b(:);
|
| | 300 | if any(found)
|
| | 301 | lia(i) = true;
|
| | 302 | locb(i) = find(found,1);
|
| | 303 | end
|
| | 304 | end
|
< 0.001 | 5 | 305 | end
|
| | 306 | else
|
| | 307 | % Use method which sorts list, then performs binary search.
|
| | 308 | % Convert to full to work in C helper.
|
| | 309 | if issparse(a)
|
| | 310 | a = full(a);
|
| | 311 | end
|
| | 312 | if issparse(b)
|
| | 313 | b = full(b);
|
| | 314 | end
|
| | 315 |
|
| | 316 | if (isreal(b))
|
| | 317 | % Find out whether list is presorted before sort
|
| | 318 | sortedlist = issorted(b(:));
|
| | 319 | if nargout > 1
|
| | 320 | if ~sortedlist
|
| | 321 | [b,idx] = sort(b(:));
|
| | 322 | end
|
| | 323 | elseif ~sortedlist
|
| | 324 | b = sort(b(:));
|
| | 325 | end
|
| | 326 | else
|
| | 327 | sortedlist = 0;
|
| | 328 | [~,idx] = sort(real(b(:)));
|
| | 329 | b = b(idx);
|
| | 330 | end
|
| | 331 |
|
| | 332 | % Use builtin helper function ISMEMBERHELPER:
|
| | 333 | % [LIA,LOCB] = ISMEMBERHELPER(A,B) Returns logical array LIA indicating
|
| | 334 | % which elements of A occur in B and a double array LOCB with the
|
| | 335 | % locations of the elements of A occuring in B. If multiple instances
|
| | 336 | % occur, the first occurence is returned. B must be already sorted.
|
| | 337 |
|
| | 338 | if ~isobject(a) && ~isobject(b) && (isnumeric(a) || ischar(a) || islogical(a))
|
| | 339 | if (isnan(b(end)))
|
| | 340 | % If NaNs detected, remove NaNs from B.
|
| | 341 | b = b(~isnan(b(:)));
|
| | 342 | end
|
| | 343 | if nargout <= 1
|
| | 344 | lia = builtin('_ismemberhelper',a,b);
|
| | 345 | else
|
| | 346 | [lia, locb] = builtin('_ismemberhelper',a,b);
|
| | 347 | end
|
| | 348 | else % a,b, are some other class like gpuArray, sym object.
|
| | 349 | lia = false(size(a));
|
| | 350 | if nargout <= 1
|
| | 351 | for i=1:numelA
|
| | 352 | lia(i) = any(a(i)==b(:)); % ANY returns logical.
|
| | 353 | end
|
| | 354 | else
|
| | 355 | for i=1:numelA
|
| | 356 | found = a(i)==b(:); % FIND returns indices for LOCB.
|
| | 357 | if any(found)
|
| | 358 | lia(i) = true;
|
| | 359 | found = find(found);
|
| | 360 | locb(i) = found(1);
|
| | 361 | end
|
| | 362 | end
|
| | 363 | end
|
| | 364 | end
|
| | 365 | if nargout > 1 && ~sortedlist
|
| | 366 | % Re-reference locb to original list if it was unsorted
|
| | 367 | locb(lia) = idx(locb(lia));
|
| | 368 | end
|
< 0.001 | 5 | 369 | end
|
< 0.001 | 5 | 370 | end
|
Other subfunctions in this file are not included in this listing.