Creating a random number based on normal distribution in matlab

I am trying to create a random number based on the normal distribution characteristics that I have (mean and standard deviation). I do not have statistics and machine learning tools.

I know that one way to do this is to randomly generate a random number rfrom 0 to 1 and find the value that gives the probability of this random number. I can do this by introducing a standard normal function

f= @(y) (1/(1*2.50663))*exp(-((y).^2)/(2*1^2))

and solution for

r=integral(f,-Inf,z)

and then extrapolate from this z-value to the final answer Xusing the equation

z=(X-mew)/sigma

But as far as I know, there is no matlab command that allows you to solve for x, where x is the limit of the integral. Is there a way to do this, or is there a better way to randomly generate this number?

+4
source share
2 answers

You can use the built-in function randn, which gives random numbers pulled from the standard normal distribution with a zero mean value and a standard deviation of 1. To change this distribution, you can multiply the output randnby the desired standard deviation, and then add the desired average value.

% Define the distribution that you'd like to get
mu = 2.5;
sigma = 2.0;

% You can any size matrix of values
sz = [10000 1];

value = (randn(sz) * sigma) + mu;

%   mean(value)
%       2.4696
%
%   std(value)
%       1.9939

, randn,

value = (randn * sigma) + mu;
+4

, :

  • (0,1)
  • ( ) (0,2 * pi), . sqrt (2) .

( , ). , , .

( ):

m = 1; n = 1e5; % desired output size
x = sqrt(-2*log(rand(m,n))).*cos(2*pi*rand(m,n));

Check:

>> mean(x)
ans =
  -0.001194631660594
>> std(x)
ans =
   0.999770464360453
>> histogram(x,41)

enter image description here

+3

Source: https://habr.com/ru/post/1662693/


All Articles