Borrar filtros
Borrar filtros

Gaussian distribution with randn

29 visualizaciones (últimos 30 días)
Joseph Lee
Joseph Lee el 14 de Nov. de 2017
Comentada: Joseph Lee el 14 de Nov. de 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 comentarios
Rik
Rik el 14 de Nov. de 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 el 14 de Nov. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Akira Agata
Akira Agata el 14 de Nov. de 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 comentarios
Rik
Rik el 14 de Nov. de 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 el 14 de Nov. de 2017
Thanks for the explanation

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 14 de Nov. de 2017
Editada: Guillaume el 14 de Nov. de 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 comentario
Joseph Lee
Joseph Lee el 14 de Nov. de 2017
+- 0.02 that can be considered the variance

Iniciar sesión para comentar.

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by