time | Calls | line |
---|
| | 1 | function this = parenAssign(this,rhs,rowIndices,colIndices,varargin)
|
| | 2 | %
|
| | 3 | % THIS = PARENASSIGN(THIS,RHS,LINEARINDICES)
|
| | 4 | % THIS = PARENASSIGN(THIS,RHS,ROWINDICES,COLINDICES)
|
| | 5 | % THIS = PARENASSIGN(THIS,RHS,ROWINDICES,COLINDICES,PAGEINDICES,...)
|
| | 6 |
|
| | 7 | % Copyright 2019 The MathWorks, Inc.
|
| | 8 |
|
| | 9 | % Only simple paren assignments come directly here; creation goes to subsasgn,
|
| | 10 | % but ultimately ends up here also. categorical has no properties, and therefore
|
| | 11 | % no multi-level paren assignments.
|
| | 12 |
|
| | 13 | import matlab.internal.datatypes.isScalarText
|
| | 14 |
|
< 0.001 | 507 | 15 | nsubs = nargin - 2;
|
< 0.001 | 507 | 16 | if nsubs == 0, error(message('MATLAB:atLeastOneIndexIsRequired')); end
|
| | 17 |
|
< 0.001 | 507 | 18 | if isnumeric(this) && isequal(this,[]) % subscripted assignment to an array that doesn't exist
|
| | 19 | this = rhs; % preserve the subclass
|
| | 20 | this.codes = zeros(0,class(rhs.codes)); % account for the number of categories in b
|
< 0.001 | 507 | 21 | end
|
< 0.001 | 507 | 22 | deleting = false; % assume for now
|
| | 23 |
|
< 0.001 | 507 | 24 | anames = this.categoryNames;
|
< 0.001 | 507 | 25 | numCatsOld = length(anames);
|
| | 26 |
|
< 0.001 | 507 | 27 | if isa(rhs,'categorical')
|
< 0.001 | 507 | 28 | bcodes = rhs.codes;
|
< 0.001 | 507 | 29 | bnames = rhs.categoryNames;
|
| | 30 | % If b is categorical, its ordinalness has to match a, and if they are
|
| | 31 | % ordinal, their categories have to match.
|
< 0.001 | 507 | 32 | if this.isOrdinal ~= rhs.isOrdinal
|
| | 33 | error(message('MATLAB:categorical:OrdinalMismatchAssign'));
|
0.002 | 507 | 34 | elseif isequal(anames,bnames)
|
| | 35 | % Identical category names => same codes class => no cast needed for acodes
|
| | 36 | else
|
| | 37 | if this.isOrdinal
|
| | 38 | error(message('MATLAB:categorical:OrdinalCategoriesMismatch'));
|
| | 39 | end
|
| | 40 | % Convert b's codes to a's codes. a's new set of categories grows only by
|
| | 41 | % the categories that are actually being assigned, and a never needs to
|
| | 42 | % care about the others in b that are not assigned.
|
| | 43 | if isscalar(rhs)
|
| | 44 | % When b is an <undefined> scalar, bcodes is already correct in
|
| | 45 | % a's codes (undefCode is the same in all categoricals) and no
|
| | 46 | % conversion is needed; otherwise, we can behave as if it only
|
| | 47 | % has the one category, and conversion to a's codes is faster.
|
| | 48 | if bcodes ~= 0 % categorical.undefCode
|
| | 49 | [bcodes,anames] = convertCodesForSubsasgn(1,bnames{bcodes},anames,this.isProtected);
|
| | 50 | end
|
| | 51 | else
|
| | 52 | [bcodes,anames] = convertCodesForSubsasgn(bcodes,bnames,anames,this.isProtected);
|
| | 53 | end
|
< 0.001 | 507 | 54 | end
|
| | 55 | elseif isScalarText(rhs) || matlab.internal.datatypes.isCharStrings(rhs)
|
| | 56 | [bcodes,bnames] = strings2codes(rhs);
|
| | 57 | [bcodes,anames] = convertCodesForSubsasgn(bcodes,bnames,anames,this.isProtected);
|
| | 58 | elseif isa(rhs, 'missing')
|
| | 59 | bcodes = zeros(size(rhs), 'uint8');
|
| | 60 | elseif isnumeric(rhs)
|
| | 61 | % Check numeric before builtin to short-circuit for performance and to
|
| | 62 | % distinguish between '' and [].
|
| | 63 | deleting = isequal(rhs,[]) && builtin('_isEmptySqrBrktLiteral',rhs);
|
| | 64 | if deleting % deletion by assignment
|
| | 65 | % Deleting elements, but the categories stay untouched. No need
|
| | 66 | % to possibly downcast a.codes with castCodes.
|
| | 67 | switch nsubs
|
| | 68 | case 1 % 1-D subscripting
|
| | 69 | this.codes(rowIndices) = [];
|
| | 70 | case 2 % 2-D subscripting
|
| | 71 | this.codes(rowIndices,colIndices) = [];
|
| | 72 | otherwise % >= 3, N-D subscripting
|
| | 73 | this.codes(rowIndices,colIndices,varargin{:}) = [];
|
| | 74 | end
|
| | 75 | else
|
| | 76 | error(message('MATLAB:categorical:InvalidRHS', class(this)));
|
| | 77 | end
|
| | 78 | else
|
| | 79 | error(message('MATLAB:categorical:InvalidRHS', class(this)));
|
< 0.001 | 507 | 80 | end
|
| | 81 |
|
< 0.001 | 507 | 82 | if ~deleting
|
| | 83 | % Upcast a's codes if necessary to account for any new categories
|
< 0.001 | 507 | 84 | if length(anames) > numCatsOld
|
| | 85 | this.codes = categorical.castCodes(this.codes,length(anames));
|
< 0.001 | 507 | 86 | end
|
< 0.001 | 507 | 87 | switch nsubs
|
< 0.001 | 507 | 88 | case 1 % 1-D subscripting
|
| | 89 | this.codes(rowIndices) = bcodes;
|
< 0.001 | 507 | 90 | case 2 % 2-D subscripting
|
0.005 | 507 | 91 | this.codes(rowIndices,colIndices) = bcodes;
|
| | 92 | otherwise % >= 3, N-D subscripting
|
| | 93 | this.codes(rowIndices,colIndices,varargin{:}) = bcodes;
|
< 0.001 | 507 | 94 | end
|
< 0.001 | 507 | 95 | this.categoryNames = anames;
|
< 0.001 | 507 | 96 | end
|
Other subfunctions in this file are not included in this listing.