Question

Can anyone help me how to write a MATLAB code to generate 100,000 random samples (1D)?

The samples should follow 2 PDFs:

  1. N(+1, 0.5)
  2. N(-1, 0.5)

where the notation N(µ,σ) indicates a Normal distribution with mean µ and standard deviation σ.

Pas de solution correcte

Autres conseils

Use the randn() function and substitute in mean and standard deviation as follows

mu + stdev.*randn(100,1)

For more information, check out the matlab help for randn()

http://www.mathworks.com/help/matlab/ref/randn.html

Alternatively, you can use MATLAB's Probability Distribution Objects which make working with probability distributions very easy.

Note this solution requires the Statistics and Machine Learning Toolbox to use makedist() and random().

% MATLAB R2018b
% Define probability distribution objects
pd1 = makedist('Normal',1,0.5');
pd2 = makedist('Normal',-1,0.5');
% Generate Samples
X1 = random(pd1,100,1);
X2 = random(pd2,100,1);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top