time | Calls | line |
---|
| | 155 | function [lia,locb] = ismemberR2012a(a,b,options)
|
| | 156 | % 'R2012a' flag implementation
|
| | 157 |
|
| | 158 | % Error check flag
|
< 0.001 | 2 | 159 | if nargin == 2
|
| 2 | 160 | byrow = false;
|
| | 161 | else
|
| | 162 | byrow = options > 0;
|
< 0.001 | 2 | 163 | end
|
| | 164 |
|
< 0.001 | 2 | 165 | doBuiltinTypes = true;
|
| | 166 | % Check that one of A and B is double if A and B are non-homogeneous. Do a
|
| | 167 | % separate check if A is a heterogeneous object and only allow a B
|
| | 168 | % that is of the same root class.
|
< 0.001 | 2 | 169 | if ~(isa(a,'handle.handle') || isa(b,'handle.handle'))
|
< 0.001 | 2 | 170 | if ~strcmpi(class(a),class(b))
|
| | 171 | if isa(a,'matlab.mixin.Heterogeneous') && isa(b,'matlab.mixin.Heterogeneous')
|
| | 172 | rootClassA = meta.internal.findHeterogeneousRootClass(a);
|
| | 173 | if isempty(rootClassA) || ~isa(b,rootClassA.Name)
|
| | 174 | error(message('MATLAB:ISMEMBER:InvalidInputsDataType',class(a),class(b)));
|
| | 175 | end
|
| | 176 | doBuiltinTypes = false;
|
| | 177 | elseif ~(strcmpi(class(a),'double') || strcmpi(class(b),'double'))
|
| | 178 | error(message('MATLAB:ISMEMBER:InvalidInputsDataType',class(a),class(b)));
|
| | 179 | end
|
| | 180 | end
|
| 2 | 181 | end
|
| | 182 |
|
< 0.001 | 2 | 183 | if ~byrow
|
< 0.001 | 2 | 184 | if ~(isa(a,'opaque') || isa(b,'opaque')) && doBuiltinTypes
|
| | 185 | % Builtin types
|
| | 186 | if nargout > 1
|
| | 187 | [lia,locb] = ismemberBuiltinTypes(a,b);
|
| | 188 | else
|
| | 189 | lia = ismemberBuiltinTypes(a,b);
|
| | 190 | end
|
| 2 | 191 | else
|
| | 192 | % Handle objects
|
< 0.001 | 2 | 193 | if nargout > 1
|
| | 194 | [lia,locb] = ismemberClassTypes(a,b);
|
< 0.001 | 2 | 195 | else
|
0.002 | 2 | 196 | lia = ismemberClassTypes(a,b);
|
| 2 | 197 | end
|
< 0.001 | 2 | 198 | end
|
| | 199 | else % 'rows' case
|
| | 200 | if ~(ismatrix(a) && ismatrix(b))
|
| | 201 | error(message('MATLAB:ISMEMBER:NotAMatrix'));
|
| | 202 | end
|
| | 203 |
|
| | 204 | [rowsA,colsA] = size(a);
|
| | 205 | [rowsB,colsB] = size(b);
|
| | 206 |
|
| | 207 | % Automatically pad strings with spaces
|
| | 208 | if ischar(a) && ischar(b)
|
| | 209 | b = [b repmat(' ',rowsB,colsA-colsB)];
|
| | 210 | a = [a repmat(' ',rowsA,colsB-colsA)];
|
| | 211 | elseif colsA ~= colsB
|
| | 212 | error(message('MATLAB:ISMEMBER:AandBColnumAgree'));
|
| | 213 | end
|
| | 214 |
|
| | 215 | % Empty check for 'rows'.
|
| | 216 | if rowsA == 0 || rowsB == 0
|
| | 217 | lia = false(rowsA,1);
|
| | 218 | locb = zeros(rowsA,1);
|
| | 219 | return
|
| | 220 | end
|
| | 221 |
|
| | 222 | % General handling for 'rows'.
|
| | 223 | [checkCast, cls] = needsCastingChecks(a, b);
|
| | 224 | needLocb = (nargout > 1) || checkCast;
|
| | 225 |
|
| | 226 | % Duplicates within the sets are eliminated
|
| | 227 | if (rowsA == 1)
|
| | 228 | uA = repmat(a,rowsB,1);
|
| | 229 | d = uA(1:end,:)==b(1:end,:);
|
| | 230 | d = all(d,2);
|
| | 231 | lia = any(d);
|
| | 232 | if nargout > 1
|
| | 233 | if lia
|
| | 234 | locb = find(d, 1, 'first');
|
| | 235 | else
|
| | 236 | locb = 0;
|
| | 237 | end
|
| | 238 | end
|
| | 239 | return;
|
| | 240 | else
|
| | 241 | if checkCast
|
| | 242 | [uA,~,icA] = unique(cast(a, cls),'rows','sorted');
|
| | 243 | else
|
| | 244 | [uA,~,icA] = unique(a,'rows','sorted');
|
| | 245 | end
|
| | 246 | end
|
| | 247 | if ~needLocb
|
| | 248 | uB = unique(b,'rows','sorted');
|
| | 249 | elseif checkCast && ~strcmp(cls, class(b))
|
| | 250 | % Find rows of B that do not change when cast.
|
| | 251 | ibC = find(all(cast(b, cls) == b, 2));
|
| | 252 | % Find the unique rows within those rows.
|
| | 253 | [uB,ib] = unique(b(ibC,:), 'rows','sorted');
|
| | 254 | % Adjust the indices to account for the removed rows.
|
| | 255 | ib = ibC(ib);
|
| | 256 | else
|
| | 257 | [uB,ib] = unique(b, 'rows','sorted');
|
| | 258 | end
|
| | 259 |
|
| | 260 | % Sort the unique elements of A and B, duplicate entries are adjacent
|
| | 261 | [sortuAuB,IndSortuAuB] = sortrows([uA;uB]);
|
| | 262 |
|
| | 263 | % Find matching entries
|
| | 264 | d = sortuAuB(1:end-1,:)==sortuAuB(2:end,:); % d indicates matching entries
|
| | 265 | d = all(d,2); % Finds the index of matching entries
|
| | 266 | ndx1 = IndSortuAuB(d); % NDX1 are locations of repeats in C
|
| | 267 |
|
| | 268 | if ~needLocb
|
| | 269 | lia = ismemberBuiltinTypes(icA,ndx1); % Find repeats among original list
|
| | 270 | else
|
| | 271 | szuA = size(uA,1);
|
| | 272 | [lia,locb] = ismemberBuiltinTypes(icA,ndx1); % Find locb by using given indices
|
| | 273 | d = find(d);
|
| | 274 | newd = d(locb(lia)); % NEWD is D for non-unique A
|
| | 275 | where = ib(IndSortuAuB(newd+1)-szuA); % Index values of uB through UNIQUE
|
| | 276 | locb(lia) = where; % Return first or last occurrence of A within B
|
| | 277 | end
|
| | 278 | if checkCast
|
| | 279 | % Post-processing step: if we matched anything in a to b only
|
| | 280 | % because of casting during the concatenation, we need to fix it.
|
| | 281 | if ~strcmp(cls, class(a))
|
| | 282 | % a may have lost precision in the concatenation.
|
| | 283 | % Find all rows that have an entry that changed in the cast.
|
| | 284 | rmA = any(cast(a, cls) ~= a, 2);
|
| | 285 | % Remove these rows from elibility.
|
| | 286 | lia(rmA) = false;
|
| | 287 | locb(rmA) = 0;
|
| | 288 | end
|
| | 289 | end
|
< 0.001 | 2 | 290 | end
|
< 0.001 | 2 | 291 | end
|
Other subfunctions in this file are not included in this listing.