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/quartiles.m

17 lines
629 B
Matlab

function q = quartiles( x )
% returns a vector with the first, second, and third quartile
% of the vector x
xs = sort( x );
if ( length( xs ) == 0 ) % no data
q = [];
elseif ( rem( length( xs ), 2 ) == 0 ) % even number of data
index = length( xs )/2;
m = (xs( index ) + xs( index+1 ))/2;
q = [ round( xs(length(xs)/4) ), m, xs(round(3*length(xs)/4)) ];
else % odd number of data
index = (length( xs ) + 1)/2;
m = xs( index );
q = [ round( xs(length(xs)/4) ), m, xs(round(3*length(xs)/4)) ];
end
end