time | Calls | line |
---|
| | 169 | function [c,ia] = setdiffR2012a(a,b,options)
|
| | 170 | % 'R2012a' flag implementation
|
| | 171 |
|
| | 172 | % flagvals = {'rows' 'sorted' 'stable'};
|
< 0.001 | 5 | 173 | if nargin == 2
|
< 0.001 | 5 | 174 | byrow = false;
|
< 0.001 | 5 | 175 | order = 'sorted';
|
| | 176 | else
|
| | 177 | byrow = (options(1) > 0);
|
| | 178 | if options(3) > 0
|
| | 179 | order = 'stable';
|
| | 180 | else % if options(2) > 0 || sum(options(2:3)) == 0)
|
| | 181 | order = 'sorted';
|
| | 182 | end
|
< 0.001 | 5 | 183 | end
|
| | 184 |
|
| | 185 | % Check that one of A and B is double if A and B are non-homogeneous. Do a
|
| | 186 | % separate check if A is a heterogeneous object and only allow a B
|
| | 187 | % that is of the same root class.
|
< 0.001 | 5 | 188 | if ~(isa(a,'handle.handle') || isa(b,'handle.handle'))
|
< 0.001 | 5 | 189 | if ~strcmpi(class(a),class(b))
|
< 0.001 | 5 | 190 | if isa(a,'matlab.mixin.Heterogeneous') && isa(b,'matlab.mixin.Heterogeneous')
|
| | 191 | rootClassA = meta.internal.findHeterogeneousRootClass(a);
|
| | 192 | if isempty(rootClassA) || ~isa(b,rootClassA.Name)
|
| | 193 | error(message('MATLAB:SETDIFF:InvalidInputsDataType',class(a),class(b)));
|
| | 194 | end
|
< 0.001 | 5 | 195 | elseif ~(strcmpi(class(a),'double') || strcmpi(class(b),'double'))
|
| | 196 | error(message('MATLAB:SETDIFF:InvalidInputsDataType',class(a),class(b)));
|
| | 197 | end
|
< 0.001 | 5 | 198 | end
|
< 0.001 | 5 | 199 | end
|
| | 200 |
|
| | 201 | % Determine if A is a row vector.
|
< 0.001 | 5 | 202 | rowvec = isrow(a);
|
| | 203 |
|
< 0.001 | 5 | 204 | sparseA = false;
|
| | 205 |
|
| | 206 | % Determine if A is sparse
|
< 0.001 | 5 | 207 | if issparse(a)
|
| | 208 | sparseA = true;
|
| | 209 | a = full(a);
|
| | 210 | end
|
< 0.001 | 5 | 211 | if issparse(b)
|
| | 212 | b = full(b);
|
| | 213 | end
|
| | 214 |
|
< 0.001 | 5 | 215 | if ~byrow
|
| | 216 | % Convert to columns.
|
< 0.001 | 5 | 217 | a = a(:);
|
< 0.001 | 5 | 218 | b = b(:);
|
| | 219 |
|
| | 220 | % Call ISMEMBER to determine list of non-matching elements of A.
|
0.024 | 5 | 221 | logUA = ~(ismember(a,b));
|
< 0.001 | 5 | 222 | c = a(logUA);
|
| | 223 |
|
| | 224 | % Call UNIQUE to remove duplicates from list of non-matches.
|
< 0.001 | 5 | 225 | if nargout <= 1
|
0.002 | 5 | 226 | c = unique(c,order);
|
| | 227 | else
|
| | 228 | [c,ndx] = unique(c,order);
|
| | 229 |
|
| | 230 | % Find indices by using logUA and NDX.
|
| | 231 | indlogUA = find(logUA);
|
| | 232 | ia = indlogUA(ndx);
|
< 0.001 | 5 | 233 | end
|
| | 234 |
|
| | 235 | % If a is a row vector, return c as a row vector.
|
< 0.001 | 5 | 236 | if rowvec
|
| | 237 | c = c.';
|
| | 238 | end
|
| | 239 | % If a is sparse then c is sparse.
|
< 0.001 | 5 | 240 | if sparseA
|
| | 241 | c = sparse(c);
|
| | 242 | end
|
| | 243 |
|
| | 244 | else % 'rows' case
|
| | 245 | if ~(ismatrix(a) && ismatrix(b))
|
| | 246 | error(message('MATLAB:SETDIFF:NotAMatrix'));
|
| | 247 | end
|
| | 248 |
|
| | 249 | [rowsA,colsA] = size(a);
|
| | 250 | [rowsB,colsB] = size(b);
|
| | 251 |
|
| | 252 | % Automatically pad strings with spaces
|
| | 253 | if ischar(a) && ischar(b)
|
| | 254 | if colsA > colsB
|
| | 255 | b = [b repmat(' ',rowsB,colsA-colsB)];
|
| | 256 | elseif colsA < colsB
|
| | 257 | a = [a repmat(' ',rowsA,colsB-colsA)];
|
| | 258 | colsA = colsB;
|
| | 259 | end
|
| | 260 | elseif colsA ~= colsB
|
| | 261 | error(message('MATLAB:SETDIFF:AandBColnumAgree'));
|
| | 262 | end
|
| | 263 |
|
| | 264 | % Handle empty arrays
|
| | 265 | if rowsA == 0
|
| | 266 | c = zeros(rowsA,colsA);
|
| | 267 | ia = zeros(0,1);
|
| | 268 | elseif colsA == 0
|
| | 269 | c = [];
|
| | 270 | ia = zeros(0,1);
|
| | 271 | % General handling
|
| | 272 | else
|
| | 273 | % Remove duplicates from A; get indices only if needed
|
| | 274 | if nargout > 1
|
| | 275 | [uA,ia] = unique(a,'rows',order);
|
| | 276 | else
|
| | 277 | uA = unique(a,'rows',order);
|
| | 278 | end
|
| | 279 |
|
| | 280 | % Create sorted list of unique A and B; want non-matching entries
|
| | 281 | [sortuAB,ndx] = sortrows([uA;b]);
|
| | 282 | [rowsC,colsC] = size(sortuAB);
|
| | 283 |
|
| | 284 | if rowsC > 1 && colsC ~= 0
|
| | 285 | % d indicates the location of non-matching entries
|
| | 286 | d = sortuAB(1:rowsC-1,:) ~= sortuAB(2:rowsC,:);
|
| | 287 | else
|
| | 288 | d = zeros(rowsC-1,0);
|
| | 289 | end
|
| | 290 |
|
| | 291 | d = any(d,2);
|
| | 292 | d(rowsC,1) = 1; % Final entry always included.
|
| | 293 | numRowsUA = size(uA,1);
|
| | 294 | d = d & ndx <= numRowsUA; % Now find only the ones in A.
|
| | 295 |
|
| | 296 | if strcmp(order, 'stable') % 'stable'
|
| | 297 | d = sort(ndx(d)); % Sort ndx(d) to maintain 'stable' order
|
| | 298 | c = uA(d,:);
|
| | 299 | else
|
| | 300 | c = sortuAB(d,:);
|
| | 301 | end
|
| | 302 |
|
| | 303 | % Find IA only if needed.
|
| | 304 | if nargout > 1
|
| | 305 | if strcmp(order, 'stable') % 'stable'
|
| | 306 | ia = ia(d);
|
| | 307 | else
|
| | 308 | ia = ia(ndx(d));
|
| | 309 | end
|
| | 310 | end
|
| | 311 | end
|
| | 312 | % If a is sparse then c is sparse.
|
| | 313 | if sparseA
|
| | 314 | c = sparse(c);
|
| | 315 | end
|
< 0.001 | 5 | 316 | end
|
< 0.001 | 5 | 317 | end
|
Other subfunctions in this file are not included in this listing.