Effacer les filtres
Effacer les filtres

Gaussian distribution with randn

30 vues (au cours des 30 derniers jours)
Joseph Lee
Joseph Lee le 14 Nov 2017
Commenté : Joseph Lee le 14 Nov 2017
Is it possible and how can i obtain a Gaussian distribution with randn for
mean= 0.126
and it varies by+- 0.02, max=0.146 and min=0.106,
to generate 1300 random values.
  2 commentaires
Rik
Rik le 14 Nov 2017
Have you read the documentation (just type doc randn)? You'll need to shift the mean, change the width of the distribution and do something about values outside your range. Think carefully about the order in which you do these.
Joseph Lee
Joseph Lee le 14 Nov 2017
I tried but generated some negative values instead, which is why i am asking how and whether is it possible or is there a better function to use.

Connectez-vous pour commenter.

Réponse acceptée

Akira Agata
Akira Agata le 14 Nov 2017
If you want to generate Gaussian distribution with the given mean and variance (not std), and then extract the values in [min max] range, the following code can do it.
va = 0.02;
mu = 0.126;
ul = 0.146;
ll = 0.106;
x = mu + randn(20000,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
x = x(idx);
x = x(1:1300); % Extract 1300 numbers
  3 commentaires
Rik
Rik le 14 Nov 2017
If your limit values are getting closer to the mean, you will need to progressively generate more values. There will be a function that estimates how many values you will need, but it is probably just easier to hard-code it into the solution given by Akira:
va = 0.02;
mu = 0.126;
ul = 0.146;
ll = 0.106;
nvals=1300;
multiplier=10;%Akira started with 15, for this example, 9 is not always enough, 10 is
x = mu + randn(multiplier*nvals,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
while sum(idx)<nvals
multiplier=multiplier+1;
x = mu + randn(multiplier*nvals,1)*sqrt(va); % Generate sufficient random numbers
idx = (ll <= x) & (x <= ul); % Extract the value in the given range [min max]
end
x = x(idx);
x = x(1:nvals); % Extract 1300 numbers
Joseph Lee
Joseph Lee le 14 Nov 2017
Thanks for the explanation

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 14 Nov 2017
Modifié(e) : Guillaume le 14 Nov 2017
By definition, a gaussian distribution covers the whole range [-∞, +∞]. If your distribution has a min and max it's not gaussian anymore.
Furthermore, a gaussian distribution is defined by a mean and a standard deviation, not a mean and a range. If a gaussian distribution has a standard deviation of 0.02, you'll still find about 32% of the samples outside of that ±0.02 range.
  1 commentaire
Joseph Lee
Joseph Lee le 14 Nov 2017
+- 0.02 that can be considered the variance

Connectez-vous pour commenter.

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by