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-2014 The MathWorks, Inc.
|
| | 30 |
|
< 0.001 | 94 | 31 | if nargin <= 2
|
| | 32 | % The only A and B allowed are character arrays or cellstr.
|
0.002 | 94 | 33 | if ~((ischar(a) || iscellstr(a)) && (ischar(b) || iscellstr(b)))
|
| | 34 | error(message('MATLAB:ISMEMBER:InputClass',class(a),class(b)));
|
< 0.001 | 94 | 35 | end
|
| | 36 | % Scalar A: no sort needed
|
0.002 | 94 | 37 | if (ischar(a) && isrow(a)) || isscalar(a)
|
< 0.001 | 94 | 38 | match = strcmp(a,b);
|
< 0.001 | 94 | 39 | lia = any(match(:));
|
< 0.001 | 94 | 40 | if nargout > 1
|
| | 41 | if lia
|
| | 42 | locb = find(match,1,'first');
|
| | 43 | else
|
| | 44 | locb = 0;
|
| | 45 | end
|
< 0.001 | 94 | 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 A or B is char, convert it to a cellstr and remove trailing spaces
|
| | 55 | if ischar(a)
|
| | 56 | a = cellstr(a);
|
| | 57 | end
|
| | 58 | if ischar(b)
|
| | 59 | b = cellstr(b);
|
| | 60 | end
|
| | 61 | % Duplicates within the sets are eliminated
|
| | 62 | [uA,~,icA] = unique(a(:),'sorted');
|
| | 63 | if nargout <= 1
|
| | 64 | uB = unique(b(:),'sorted');
|
| | 65 | else
|
| | 66 | [uB,ib] = unique(b(:),'sorted');
|
| | 67 | end
|
| | 68 | % Sort the unique elements of A and B, duplicate entries are adjacent
|
| | 69 | [sortuAuB,IndSortuAuB] = sort([uA;uB]);
|
| | 70 | % d indicates the indices matching entries
|
| | 71 | d = strcmp(sortuAuB(1:end-1),sortuAuB(2:end));
|
| | 72 | indReps = IndSortuAuB(d); % Find locations of repeats
|
| | 73 | if nargout <= 1
|
| | 74 | lia = ismember(icA,indReps); % Find repeats among original list
|
| | 75 | else
|
| | 76 | szuA = size(uA,1);
|
| | 77 | d = find(d);
|
| | 78 | [lia,locb] = ismember(icA,indReps); % Find locb by using given indices
|
| | 79 | newd = d(locb(lia)); % NEWD is D for non-unique A
|
| | 80 | where = ib(IndSortuAuB(newd+1)-szuA);
|
| | 81 | locb(lia) = where;
|
| | 82 | end
|
| | 83 | lia = reshape(lia,size(a));
|
| | 84 | if nargout > 1
|
| | 85 | locb = reshape(locb,size(a));
|
| | 86 | end
|
< 0.001 | 94 | 87 | end
|
| | 88 | elseif nargin == 3
|
| | 89 | % ISMEMBER(a,b,'legacy'), ISMEMBER(a,b,'R2012a')
|
| | 90 | if nargout < 2
|
| | 91 | lia = cellismemberlegacy(a,b,flag1);
|
| | 92 | else
|
| | 93 | [lia,locb] = cellismemberlegacy(a,b,flag1);
|
| | 94 | end
|
| | 95 | else
|
| | 96 | % ISMEMBER(a,b,'rows','legacy'), ISMEMBER(a,b,'rows','R2012a')
|
| | 97 | if nargout < 2
|
| | 98 | lia = cellismemberlegacy(a,b,flag1,flag2);
|
| | 99 | else
|
| | 100 | [lia,locb] = cellismemberlegacy(a,b,flag1,flag2);
|
| | 101 | end
|
< 0.001 | 94 | 102 | end
|
Other subfunctions in this file are not included in this listing.