gusucode.com > datatypes 工具箱matlab源码程序 > datatypes/@categorical/sortrows.m
function [b,varargout] = sortrows(a,col) %SORTROWS Sort rows of a categorical array. % B = SORTROWS(A) sorts the rows of the 2-dimensional categorical matrix A in % ascending order as a group. B is a categorical array with the same % categories as A. % % B = SORTROWS(A,COL) sorts A based on the columns specified in the vector % COL. If an element of COL is positive, the corresponding column in A will % be sorted in ascending order; if an element of COL is negative, the % corresponding column in A will be sorted in descending order. For example, % SORTROWS(A,[2 -3]) sorts the rows of A first in ascending order for the % second column, and then by descending order for the third column. % % [B,I] = SORTROWS(A) and [B,I] = SORTROWS(A,COL) also returns an index % matrix I such that B = A(I,:). % % Undefined elements are sorted to the end. % % See also ISSORTED, SORT. % Copyright 2006-2015 The MathWorks, Inc. import matlab.internal.categoricalUtils.categoricalsortrows; acodes = a.codes; nCategories = numel(a.categoryNames); try % CATEGORICALSORTROWS uses counting sort, which is fast only for uint8 % and uint16 but slower than builtin SORTROWS for larger types if isa(acodes, 'uint8') || isa(acodes, 'uint16') if nargin == 1 [bcodes,varargout{1:nargout-1}] = categoricalsortrows(acodes,nCategories); else [bcodes,varargout{1:nargout-1}] = categoricalsortrows(acodes,nCategories,col); end else % acodes is 'uint32' or 'uint64' % Make sure <undefined> sorts to the end when calling builtin SORTROWS acodes(acodes == categorical.undefCode) = invalidCode(acodes); if nargin == 1 [bcodes,varargout{1:nargout-1}] = sortrows(acodes); else [bcodes,varargout{1:nargout-1}] = sortrows(acodes,col); end bcodes(bcodes == invalidCode(bcodes)) = a.undefCode; % set invalidCode back to <undefined> code end catch ME throw(ME); end b = a; % preserve subclass b.codes = bcodes;