Creating Gaussian random variable with MATLAB

Crunkyyy picture Crunkyyy · Oct 27, 2012 · Viewed 16.9k times · Source

By using randn function I want to create a Gaussian random variable X such that X ~ N(2,4) and plot this simulated PDF together with theoretic curve.

Answer

Maurits picture Maurits · Oct 27, 2012

Matlab randn generates realisations from a normal distribution with zero mean and a standard deviation of 1. Samples from any other normal distribution can simply be generated via:

numSamples = 1000;
mu = 2;
sigma = 4;
samples = mu + sigma.*randn(numSamples, 1);

You can verify this by plotting the histogram:

figure;hist(samples(:));

See the matlab help.