Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.01 KB | None | 0 0
  1. function x = generate_nums_ber(n,p,m)
  2. % GENERATE_BER - generate Ber(p) outcomes m times
  3. %  
  4.  
  5. %Create vector of size m, go through loop m times
  6. x=zeros(1,m); % 1 x m vector of zeros for storing outcomes
  7. for j=1:m    
  8.   successes = 0;
  9.   for i = 1:n
  10.       u = rand;
  11.       if(u>= 1-p)
  12.           successes= successes+1;
  13.       end
  14.   end
  15.   x(j)= successes;
  16. end
  17.  
  18. % alternative to loop above: x=(rand(1,m)>=1-p);
  19.  
  20. % plot normalised histogram of outcomes versus theoretical pmf
  21.  
  22. close all;  % close all previous plots
  23.  
  24. alphabet_x=[0,1];        % 1 x 2 vector with alphabet of r.v.
  25. hx=histc(x,alphabet_x);  % compute histogram of outcomes x on alphabet (i.e. how many 0s and 1s)
  26. stem(alphabet_x,hx/m);   % plot normalised histogram on alphabet
  27. hold on;                 % keep this figure when plotting next
  28. px=[1-p,p];              % theoretical pmf of Ber(p)
  29. stem(alphabet_x,px,'rs'); % plot pmf
  30.  
  31. axis([-1 2 0 1]);                  % widen axis of figure
  32. legend('empirical','theoretical'); % add legend to figure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement