gusucode.com > datatypes 工具箱matlab源码程序 > datatypes/@categorical/median.m
function b = median(a,dim) %MEDIAN Median value of a categorical array. % B = MEDIAN(A) returns the median of the elements in the ordinal % categorical array A. For vectors, B is a categorical array containing % the median element, with the same categories as A. For matrices, B is % a categorical vector containing the median value of each column of A. % For N-D arrays, B is the median value of the elements along the first % non-singleton dimension of A. % % A must be an ordinal categorical array. % % If A contains an even number of elements along the working dimension, % the median value is the category midway between the two middle data % values, or is the larger of the two categories midway between the two % middle data values. % % MEDIAN(A,DIM) takes the median along the dimension DIM of A. % % See also MEAN, MIN, MAX, MODE. % Copyright 2014-2015 The MathWorks, Inc. narginchk(1, 2); % Check to make sure the categorical is ordinal if ~isordinal(a) error(message('MATLAB:categorical:median:NotOrdinal')); end acodes = a.codes; % Rely on built-in's NaN handling if input contains any <undefined> elements. acodes = categorical.castCodesForBuiltins(acodes); % Rely on median's behavior with dim vs. without, especially for empty input try if nargin == 1 bcodes = median(acodes); else bcodes = median(acodes,dim); end catch ME throw(ME); end if isfloat(bcodes) % Cast back to integer codes, including NaN -> <undefined> bcodes = categorical.castCodes(bcodes,length(a.categoryNames)); end b = a; % preserve subclass b.codes = bcodes;