time | Calls | line |
---|
| | 1 | function [lia,locb] = ismember(A,B,flag1,flag2)
|
| | 2 | %ISMEMBER True for set member.
|
| | 3 | % LIA = ISMEMBER(A,B) for 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 = ISMEMBER(A,B,'rows') for matrices A and B with the same number
|
| | 8 | % of columns, returns a vector containing true where the rows of A are
|
| | 9 | % also rows of B and false otherwise.
|
| | 10 | %
|
| | 11 | % [LIA,LOCB] = ISMEMBER(A,B) also returns an array LOCB containing the
|
| | 12 | % lowest absolute index in B for each element in A which is a member of
|
| | 13 | % B and 0 if there is no such index.
|
| | 14 | %
|
| | 15 | % [LIA,LOCB] = ISMEMBER(A,B,'rows') also returns a vector LOCB containing
|
| | 16 | % the lowest absolute index in B for each row in A which is a member
|
| | 17 | % of B and 0 if there is no such index.
|
| | 18 | %
|
| | 19 | % The behavior of ISMEMBER has changed. This includes:
|
| | 20 | % - occurrence of indices in LOCB switched from highest to lowest
|
| | 21 | % - tighter restrictions on combinations of classes
|
| | 22 | %
|
| | 23 | % If this change in behavior has adversely affected your code, you may
|
| | 24 | % preserve the previous behavior with:
|
| | 25 | %
|
| | 26 | % [LIA,LOCB] = ISMEMBER(A,B,'legacy')
|
| | 27 | % [LIA,LOCB] = ISMEMBER(A,B,'rows','legacy')
|
| | 28 | %
|
| | 29 | % Examples:
|
| | 30 | %
|
| | 31 | % a = [9 9 8 8 7 7 7 6 6 6 5 5 4 4 2 1 1 1]
|
| | 32 | % b = [1 1 1 3 3 3 3 3 4 4 4 4 4 9 9 9]
|
| | 33 | %
|
| | 34 | % [lia1,locb1] = ismember(a,b)
|
| | 35 | % % returns
|
| | 36 | % lia1 = [1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1]
|
| | 37 | % locb1 = [14 14 0 0 0 0 0 0 0 0 0 0 9 9 0 1 1 1]
|
| | 38 | %
|
| | 39 | % [lia,locb] = ismember([1 NaN 2 3],[3 4 NaN 1])
|
| | 40 | % % NaNs compare as not equal, so this returns
|
| | 41 | % lia = [1 0 0 1], locb = [4 0 0 1]
|
| | 42 | %
|
| | 43 | % Class support for inputs A and B, where A and B must be of the same
|
| | 44 | % class unless stated otherwise:
|
| | 45 | % - logical, char, all numeric classes (may combine with double arrays)
|
| | 46 | % - cell arrays of strings (may combine with char arrays)
|
| | 47 | % -- 'rows' option is not supported for cell arrays
|
| | 48 | % - objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE
|
| | 49 | % -- including heterogeneous arrays derived from the same root class
|
| | 50 | %
|
| | 51 | % See also ISMEMBERTOL, INTERSECT, UNION, UNIQUE, UNIQUETOL, SETDIFF,
|
| | 52 | % SETXOR, SORT, SORTROWS.
|
| | 53 |
|
| | 54 | % Copyright 1984-2018 The MathWorks, Inc.
|
| | 55 |
|
| | 56 | % Convert string flags to char flags to dispatch to the right method
|
< 0.001 | 7 | 57 | if (nargin == 3) && isstring(flag1)
|
| | 58 | flag1 = convertFlag(flag1);
|
| | 59 | [lia, locb] = ismember(A, B, flag1);
|
| | 60 | return;
|
| | 61 | end
|
< 0.001 | 7 | 62 | if (nargin == 4) && (isstring(flag1) || isstring(flag2))
|
| | 63 | if isstring(flag1)
|
| | 64 | flag1 = convertFlag(flag1);
|
| | 65 | end
|
| | 66 | if isstring(flag2)
|
| | 67 | flag2 = convertFlag(flag2);
|
| | 68 | end
|
| | 69 | [lia, locb] = ismember(A, B, flag1, flag2);
|
| | 70 | return;
|
| | 71 | end
|
| | 72 |
|
| 7 | 73 | if (isstring(A) || isstring(B))
|
| | 74 | if ~ischar(A) && ~iscellstr(A) && ~isstring(A)
|
| | 75 | firstInput = getString(message('MATLAB:string:FirstInput'));
|
| | 76 | error(message('MATLAB:string:MustBeCharCellArrayOrString', firstInput));
|
| | 77 | elseif ~ischar(B) && ~iscellstr(B) && ~isstring(B)
|
| | 78 | secondInput = getString(message('MATLAB:string:SecondInput'));
|
| | 79 | error(message('MATLAB:string:MustBeCharCellArrayOrString', secondInput));
|
| | 80 | end
|
| | 81 | A = string(A);
|
| | 82 | B = string(B);
|
| | 83 | end
|
| | 84 |
|
< 0.001 | 7 | 85 | if nargin <= 2
|
< 0.001 | 7 | 86 | if nargout < 2
|
0.006 | 7 | 87 | lia = ismemberR2012a(A,B);
|
| | 88 | else
|
| | 89 | [lia,locb] = ismemberR2012a(A,B);
|
< 0.001 | 7 | 90 | end
|
| | 91 | else
|
| | 92 | % acceptable combinations, with optional inputs denoted in []
|
| | 93 | % ismember(A,B, ['rows'], ['legacy'/'R2012a'])
|
| | 94 | nflagvals = 3;
|
| | 95 | flagvals = ["rows", "legacy", "R2012a"];
|
| | 96 | % When a flag is found, note the index into varargin where it was found
|
| | 97 | flaginds = zeros(1,nflagvals);
|
| | 98 | for i = 3:nargin
|
| | 99 | if i == 3
|
| | 100 | flag = flag1;
|
| | 101 | else
|
| | 102 | flag = flag2;
|
| | 103 | end
|
| | 104 | assert(~isstring(flag));
|
| | 105 | foundflag = matlab.internal.math.partialMatchString(flag,flagvals);
|
| | 106 | if ~any(foundflag)
|
| | 107 | if ischar(flag)
|
| | 108 | error(message('MATLAB:ISMEMBER:UnknownFlag',flag));
|
| | 109 | else
|
| | 110 | error(message('MATLAB:ISMEMBER:UnknownInput'));
|
| | 111 | end
|
| | 112 | end
|
| | 113 | % Only 1 occurrence of each allowed flag value
|
| | 114 | if flaginds(foundflag)
|
| | 115 | error(message('MATLAB:ISMEMBER:RepeatedFlag',flag));
|
| | 116 | end
|
| | 117 | flaginds(foundflag) = i;
|
| | 118 | end
|
| | 119 |
|
| | 120 | % Only 1 of each of the paired flags
|
| | 121 | if flaginds(2) && flaginds(3)
|
| | 122 | error(message('MATLAB:ISMEMBER:BehaviorConflict'))
|
| | 123 | end
|
| | 124 | % 'legacy' and 'R2012a' flags must be trailing
|
| | 125 | if flaginds(2) && flaginds(2)~=nargin
|
| | 126 | error(message('MATLAB:ISMEMBER:LegacyTrailing'))
|
| | 127 | end
|
| | 128 | if flaginds(3) && flaginds(3)~=nargin
|
| | 129 | error(message('MATLAB:ISMEMBER:R2012aTrailing'))
|
| | 130 | end
|
| | 131 |
|
| | 132 | if flaginds(3) % trailing 'R2012a' specified
|
| | 133 | if nargout < 2
|
| | 134 | lia = ismemberR2012a(A,B,logical(flaginds(1)));
|
| | 135 | else
|
| | 136 | [lia,locb] = ismemberR2012a(A,B,logical(flaginds(1)));
|
| | 137 | end
|
| | 138 | elseif flaginds(2) % trailing 'legacy' specified
|
| | 139 | if nargout < 2
|
| | 140 | lia = ismemberlegacy(A,B,logical(flaginds(1)));
|
| | 141 | else
|
| | 142 | [lia,locb] = ismemberlegacy(A,B,logical(flaginds(1)));
|
| | 143 | end
|
| | 144 | else % 'R2012a' (default behavior)
|
| | 145 | if nargout < 2
|
| | 146 | lia = ismemberR2012a(A,B,logical(flaginds(1)));
|
| | 147 | else
|
| | 148 | [lia,locb] = ismemberR2012a(A,B,logical(flaginds(1)));
|
| | 149 | end
|
| | 150 | end
|
| 7 | 151 | end
|
< 0.001 | 7 | 152 | end
|
Other subfunctions in this file are not included in this listing.