% eda03_02
% Illustrate the calculatioon of the median of a
% distribution p(d)

% Illustrative distribution p(d)
Dd = 1.0;   % sampling interval
d = Dd*[0:14]';  % vector of evenly-spaced d's
p = [5.00, 15.00, 10.00, 10.00, 10.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00]';

% normalize fistribution so its integral is unity
p = p./(Dd*sum(p));

% cumulative distribution
pc = Dd*cumsum(p);

% find first element of pc that's bigger than 0.5
for i=[1:length(p)]
    if( pc(i) > 0.5 )
        themedian = d(i);
        break;
    end
end
    
% output to command window
disp(sprintf('median = %f', themedian));