필터 지우기
필터 지우기

Gaussian distribution with randn

조회 수: 30 (최근 30일)
Joseph Lee
Joseph Lee 2017년 11월 14일
댓글: Joseph Lee 2017년 11월 14일
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
Rik
Rik 2017년 11월 14일
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 2017년 11월 14일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Akira Agata
Akira Agata 2017년 11월 14일
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
Rik
Rik 2017년 11월 14일
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 2017년 11월 14일
Thanks for the explanation

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2017년 11월 14일
편집: Guillaume 2017년 11월 14일
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
Joseph Lee
Joseph Lee 2017년 11월 14일
+- 0.02 that can be considered the variance

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by