Advertisement
makispaiktis

Normal distribution histogram

May 29th, 2022 (edited)
1,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Data
  5. avg = 10;
  6. sigma = 1;
  7. N = 10^4;
  8. values = avg + sigma .* randn(1, N);
  9. figure();
  10. histogram(values, 100);
  11. title('100 intervals histogram');
  12.  
  13. % For the 2nd histogram
  14. mult = 3;
  15. offset = - mult;
  16. edges = zeros(1, 2 * mult + 1);
  17. for i = 1 : length(edges)
  18.     edges(i) = avg + offset * sigma;
  19.     offset = offset + 1;
  20. end
  21. edges;
  22.  
  23. % Statistics
  24. % 1. Numbers per edges first
  25. num = zeros(1, 2*mult);
  26. for i = 1 : length(values)
  27.     value = values(i);
  28.     for j = 1 : length(edges) - 1
  29.         if value > edges(j) && value < edges(j+1)
  30.             num(j) = num(j) + 1;
  31.         end
  32.     end
  33. end
  34. num;
  35.  
  36. % 2. Percentages
  37. perc = zeros(1, mult);
  38. for i = 1 : length(perc)
  39.     perc(i) = 100 * (num(i) + num(2*mult + 1 - i)) / N;
  40. end
  41. perc = fliplr(perc);
  42. for i = 1 : length(perc)
  43.     disp("Values in interval [μ-" + num2str(i) + "*σ, μ + " + num2str(i) + "*σ] : " + num2str(perc(i)) + " %");
  44. end
  45. perc;
  46. figure();
  47. histogram(values, edges);
  48. title('Histogram');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement