Azzera filtri
Azzera filtri

Gaussian distribution with randn

30 visualizzazioni (ultimi 30 giorni)
Joseph Lee
Joseph Lee il 14 Nov 2017
Commentato: Joseph Lee il 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 Commenti
Rik
Rik il 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 il 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.

Accedi per commentare.

Risposta accettata

Akira Agata
Akira Agata il 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 Commenti
Rik
Rik il 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 il 14 Nov 2017
Thanks for the explanation

Accedi per commentare.

Più risposte (1)

Guillaume
Guillaume il 14 Nov 2017
Modificato: Guillaume il 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.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by