This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/statistics/code/mymedian.m

14 lines
508 B
Matlab

function m = mymedian( x )
% returns the median of the vector x
xs = sort( x );
if ( length( xs ) == 0 ) % empty input vector
m = NaN;
elseif ( rem( length( xs ), 2 ) == 0 ) % even number of data values
index = length( xs )/2;
m = (xs( index ) + xs( index+1 ))/2; % average the two central elements
else % odd number of data values
index = (length( xs ) + 1)/2; % take the middle element
m = xs( index );
end
end