% Script to compare CDF of entries of a matrix of random elements created
% using the Matlab randn function with the theoretical CDF

X = randn(100,10);  % 100x10 matrix of n(0,1) normally distributed randoms
xbin = -4:0.1:4; % Bin centers
hist(X(:),xbin);% Plot frequency in each equal-width bin

% Compare with empirical PDF
dxbin = xbin(2)-xbin(1);
PDF = exp(-xbin.^2/2)/sqrt(2*pi); % Theoretical PDF
Ntot = length(X(:)); % Number of matrix entries
NPDF = PDF*Ntot*dxbin; 
hold on
plot(xbin,NPDF,'r-');  % Plot the exact PDF scaled to the data/bin size in red
xlabel('x')
ylabel('Frequency')
legend('Sample','Theoretical')
hold off
