Filter löschen
Filter löschen

Gaussian distribution with randn

30 Ansichten (letzte 30 Tage)
Joseph Lee
Joseph Lee am 14 Nov. 2017
Kommentiert: Joseph Lee am 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 Kommentare
Rik
Rik am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Akira Agata
Akira Agata am 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 Kommentare
Rik
Rik am 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 am 14 Nov. 2017
Thanks for the explanation

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 14 Nov. 2017
Bearbeitet: Guillaume am 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 Kommentar
Joseph Lee
Joseph Lee am 14 Nov. 2017
+- 0.02 that can be considered the variance

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Random Number Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by