Understanding the following line of code regarding Gaussian distribution.
Mostra commenti meno recenti
Hello all, I am trying to plot PDF of Gaussian distribution and for that I came across the following code:
In this code, I understand every line except the one which is commented as "Not understood".
I also observed that if we omit this sentence then also code works properly.
Any help in this regard will be highly appreciated.
clc;
clear all;
close all;
% Number of samples
num_samples = 1000;
% Generate random samples from Gaussian distribution
mu_gaussian = 0; % Mean
sigma_gaussian = 1; % Standard deviation
samples_gaussian = mu_gaussian + sigma_gaussian * randn(num_samples, 1); % Not understood
x = linspace(-5, 5, 1000);
pdf_gaussian = (1 / (sigma_gaussian * sqrt(2*pi))) * exp(-(x - mu_gaussian).^2 / (2 * sigma_gaussian^2));
cdf_gaussian = 0.5 * (1 + erf((x - mu_gaussian) / (sigma_gaussian * sqrt(2))));
% Plotting PDFs
figure;
subplot(3, 1, 1);
plot(x, pdf_gaussian, 'b', 'LineWidth', 2);
title('Gaussian Distribution (PDF)');
xlabel('x');
ylabel('Probability Density');
2 Commenti
Voss
il 3 Ago 2023
See the definitions of standard normal and general normal distributions here:
Bruno Luong
il 3 Ago 2023
Modificato: Bruno Luong
il 3 Ago 2023
Operations on a single normal variable
first bullet aX + b ...
Risposta accettata
Più risposte (1)
It does precisely what the comment ahead of it says it does -- generates a set of psuedo-random normally distributed values with mean mu_gaussian, std deviation sigma_gaussian. Since those are set to 0,1 in the given code, it's the same as what randn returns, but this lets one change the location and dispersion generally.
The code does the same thing whether it is/is not there because while the array was generated, it's never used for anything in the given code; the editor will point that out for you...unless it is used later, of course, but in that case then you would discover that if you were to comment it out and the code tried to reference it. So, one presumes it was "just one of those things" that the original coder had an idea of using/needing a sampled array and then changed mind/direction and never removed the superfluous definition. Or, maybe they never got done...we have no way to know.
Categorie
Scopri di più su Uniform Distribution (Continuous) in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

