time | calls | line |
---|
| | 124 | function [lia,locb] = ismemberR2012a(a,b,options)
|
| | 125 | % 'R2012a' flag implementation
|
| | 126 |
|
| | 127 | % Error check flag
|
| 9 | 128 | if nargin == 2
|
| 9 | 129 | byrow = false;
|
| | 130 | else
|
| | 131 | byrow = options > 0;
|
| | 132 | end
|
| | 133 |
|
| 9 | 134 | doBuiltinTypes = true;
|
| | 135 | % Check that one of A and B is double if A and B are non-homogeneous. Do a
|
| | 136 | % separate check if A is a heterogeneous object and only allow a B
|
| | 137 | % that is of the same root class.
|
| 9 | 138 | if ~(isa(a,'handle.handle') || isa(b,'handle.handle'))
|
| 9 | 139 | if ~strcmpi(class(a),class(b))
|
| | 140 | if isa(a,'matlab.mixin.Heterogeneous') && isa(b,'matlab.mixin.Heterogeneous')
|
| | 141 | rootClassA = meta.internal.findHeterogeneousRootClass(a);
|
| | 142 | if isempty(rootClassA) || ~isa(b,rootClassA.Name)
|
| | 143 | error(message('MATLAB:ISMEMBER:InvalidInputsDataType',class(a),class(b)));
|
| | 144 | end
|
| | 145 | doBuiltinTypes = false;
|
| | 146 | elseif ~(strcmpi(class(a),'double') || strcmpi(class(b),'double'))
|
| | 147 | error(message('MATLAB:ISMEMBER:InvalidInputsDataType',class(a),class(b)));
|
| | 148 | end
|
| | 149 | end
|
| 9 | 150 | end
|
| | 151 |
|
| 9 | 152 | if ~byrow
|
| 9 | 153 | if ~(isa(a,'opaque') || isa(b,'opaque')) && doBuiltinTypes
|
| | 154 | % Builtin types
|
| 9 | 155 | if nargout > 1
|
| | 156 | [lia,locb] = ismemberBuiltinTypes(a,b);
|
| 9 | 157 | else
|
| 9 | 158 | lia = ismemberBuiltinTypes(a,b);
|
| 9 | 159 | end
|
| | 160 | else
|
| | 161 | % Handle objects
|
| | 162 | if nargout > 1
|
| | 163 | [lia,locb] = ismemberClassTypes(a,b);
|
| | 164 | else
|
| | 165 | lia = ismemberClassTypes(a,b);
|
| | 166 | end
|
| | 167 | end
|
| | 168 | else % 'rows' case
|
| | 169 | if ~(ismatrix(a) && ismatrix(b))
|
| | 170 | error(message('MATLAB:ISMEMBER:NotAMatrix'));
|
| | 171 | end
|
| | 172 |
|
| | 173 | [rowsA,colsA] = size(a);
|
| | 174 | [rowsB,colsB] = size(b);
|
| | 175 |
|
| | 176 | % Automatically pad strings with spaces
|
| | 177 | if ischar(a) && ischar(b),
|
| | 178 | b = [b repmat(' ',rowsB,colsA-colsB)];
|
| | 179 | a = [a repmat(' ',rowsA,colsB-colsA)];
|
| | 180 | elseif colsA ~= colsB
|
| | 181 | error(message('MATLAB:ISMEMBER:AandBColnumAgree'));
|
| | 182 | end
|
| | 183 |
|
| | 184 | % Empty check for 'rows'.
|
| | 185 | if rowsA == 0 || rowsB == 0
|
| | 186 | lia = false(rowsA,1);
|
| | 187 | locb = zeros(rowsA,1);
|
| | 188 | return
|
| | 189 | end
|
| | 190 |
|
| | 191 | % General handling for 'rows'.
|
| | 192 |
|
| | 193 | % Duplicates within the sets are eliminated
|
| | 194 | if (rowsA == 1)
|
| | 195 | uA = repmat(a,rowsB,1);
|
| | 196 | d = uA(1:end,:)==b(1:end,:);
|
| | 197 | d = all(d,2);
|
| | 198 | lia = any(d);
|
| | 199 | if nargout > 1
|
| | 200 | if lia
|
| | 201 | locb = find(d, 1, 'first');
|
| | 202 | else
|
| | 203 | locb = 0;
|
| | 204 | end
|
| | 205 | end
|
| | 206 | return;
|
| | 207 | else
|
| | 208 | [uA,~,icA] = unique(a,'rows','sorted');
|
| | 209 | end
|
| | 210 | if nargout <= 1
|
| | 211 | uB = unique(b,'rows','sorted');
|
| | 212 | else
|
| | 213 | [uB,ib] = unique(b,'rows','sorted');
|
| | 214 | end
|
| | 215 |
|
| | 216 | % Sort the unique elements of A and B, duplicate entries are adjacent
|
| | 217 | [sortuAuB,IndSortuAuB] = sortrows([uA;uB]);
|
| | 218 |
|
| | 219 | % Find matching entries
|
| | 220 | d = sortuAuB(1:end-1,:)==sortuAuB(2:end,:); % d indicates matching entries
|
| | 221 | d = all(d,2); % Finds the index of matching entries
|
| | 222 | ndx1 = IndSortuAuB(d); % NDX1 are locations of repeats in C
|
| | 223 |
|
| | 224 | if nargout <= 1
|
| | 225 | lia = ismemberBuiltinTypes(icA,ndx1); % Find repeats among original list
|
| | 226 | else
|
| | 227 | szuA = size(uA,1);
|
| | 228 | [lia,locb] = ismemberBuiltinTypes(icA,ndx1); % Find locb by using given indices
|
| | 229 | d = find(d);
|
| | 230 | newd = d(locb(lia)); % NEWD is D for non-unique A
|
| | 231 | where = ib(IndSortuAuB(newd+1)-szuA); % Index values of uB through UNIQUE
|
| | 232 | locb(lia) = where; % Return first or last occurrence of A within B
|
| | 233 | end
|
| | 234 | end
|
| 9 | 235 | end
|
Other subfunctions in this file are not included in this listing.