time | Calls | line |
|---|
| | 1 | function [lia,locb] = ismember(a,b,flag1,flag2)
|
| | 2 | %ISMEMBER True for set member.
|
| | 3 | % LIA = ISMEMBER(A,B) for cell arrays A and B returns an array of the same
|
| | 4 | % size as A containing true where the elements of A are in B and false
|
| | 5 | % otherwise.
|
| | 6 | %
|
| | 7 | % [LIA,LOCB] = ISMEMBER(A,B) also returns an array LOCB containing the
|
| | 8 | % lowest absolute index in B for each element in A which is a member of
|
| | 9 | % B and 0 if there is no such index.
|
| | 10 | %
|
| | 11 | % Inputs A and B must be strings or cell arrays of strings.
|
| | 12 | %
|
| | 13 | % The behavior of ISMEMBER has changed. This includes:
|
| | 14 | % - occurrence of indices in LOCB switched from highest to lowest
|
| | 15 | % If this change in behavior has adversely affected your code, you may
|
| | 16 | % preserve the previous behavior with:
|
| | 17 | % [LIA,LOCB] = ISMEMBER(A,B,'legacy')
|
| | 18 | %
|
| | 19 | % Example:
|
| | 20 | %
|
| | 21 | % a = {'red','green'}
|
| | 22 | % b = {'gray','blue','red','orange'}
|
| | 23 | % [lia,locb] = ismember(a,b)
|
| | 24 | %
|
| | 25 | % % returns lia = [1 0] and locb = [3 0]
|
| | 26 | %
|
| | 27 | % See also UNIQUE, UNION, INTERSECT, SETDIFF, SETXOR, SORT, SORTROWS.
|
| | 28 |
|
| | 29 | % Copyright 1984-2020 The MathWorks, Inc.
|
| | 30 |
|
< 0.001 | 7 | 31 | if nargin <= 2
|
| | 32 | % The only A and B allowed are character arrays or cellstr.
|
< 0.001 | 7 | 33 | if ~((ischar(a) || iscellstr(a)) && (ischar(b) || iscellstr(b))) %#ok<ISCLSTR>
|
| | 34 | error(message('MATLAB:ISMEMBER:InputClass',class(a),class(b)));
|
< 0.001 | 7 | 35 | end
|
| | 36 | % Scalar A: no sort needed
|
0.001 | 7 | 37 | if (ischar(a) && isrow(a)) || isscalar(a)
|
< 0.001 | 7 | 38 | match = strcmp(a,b);
|
< 0.001 | 7 | 39 | lia = any(match(:));
|
< 0.001 | 7 | 40 | if nargout > 1
|
| | 41 | if lia
|
| | 42 | locb = find(match,1,'first');
|
| | 43 | else
|
| | 44 | locb = 0;
|
| | 45 | end
|
< 0.001 | 7 | 46 | end
|
| | 47 | % Scalar B: no sort needed
|
| | 48 | elseif (ischar(b) && isrow(b)) || isscalar(b)
|
| | 49 | lia = strcmp(a,b);
|
| | 50 | if nargout > 1
|
| | 51 | locb = double(lia);
|
| | 52 | end
|
| | 53 | else
|
| | 54 | if ischar(a)
|
| | 55 | a = cellstr(a);
|
| | 56 | end
|
| | 57 | if ischar(b)
|
| | 58 | b = cellstr(b);
|
| | 59 | end
|
| | 60 | [lia,locb] = matlab.internal.math.ismemberCellHelper(a,b);
|
< 0.001 | 7 | 61 | end
|
| | 62 | elseif nargin == 3
|
| | 63 | % ISMEMBER(a,b,'legacy'), ISMEMBER(a,b,'R2012a')
|
| | 64 | if nargout < 2
|
| | 65 | lia = cellismemberlegacy(a,b,flag1);
|
| | 66 | else
|
| | 67 | [lia,locb] = cellismemberlegacy(a,b,flag1);
|
| | 68 | end
|
| | 69 | else
|
| | 70 | % ISMEMBER(a,b,'rows','legacy'), ISMEMBER(a,b,'rows','R2012a')
|
| | 71 | if nargout < 2
|
| | 72 | lia = cellismemberlegacy(a,b,flag1,flag2);
|
| | 73 | else
|
| | 74 | [lia,locb] = cellismemberlegacy(a,b,flag1,flag2);
|
| | 75 | end
|
< 0.001 | 7 | 76 | end
|
Other subfunctions in this file are not included in this listing.