% ch2Examples.m

% This function countLTEq(x,y) counts how many element of x are
%  less than each element of y. It could be substituted with the 
%  function count.m used in the textbook.
countLTEq = @(x,y)  sum(repmat(x(:),1,length(y))<=repmat(y(:).',length(x),1));

n = 5; s = 0:n;
mypmf = [.1 .3 .1 .1 .3 .1] % define PMF
mycdf = cumsum(mypmf) % derive CDF (discrete version)
figure(1), stem(s,mypmf), axis square % show 
figure(2), stem(s,mycdf), axis square
 hold on, plot(s,mycdf,'-o'), hold off
figure(3), plot(mycdf,s,'-o'), axis square
 hold on, stem(mycdf,s), hold off

myrvEx = countLTEq(mycdf,rand(1000,1));
figure(4), hist(myrvEx,s), axis square
figure(1)
