Reputation: 27
I have to develop normpdf MATLAB function in C++ environment. But I got a problem..
normpdf(X, u, sigma) in MATLAB is similar with normal_distribution gaussian(u, sigma);
where is X values? It means I want the gaussian function not gaussian random. How I sample a output value with gaussian distribution?
Thanks.
Upvotes: 1
Views: 862
Reputation: 20080
You could easily write normpdf yourself
inline double squared(double x)
{
return x*x;
}
double normpdf(double x, double u, double s) {
return (1.0/(s*sqrt(2.0*M_PI)))*exp(-0.5*squared(x-u)/squared(s));
}
UPDATE
This one might be a bit faster, less one multiplication
const double ONE_OVER_SQRT_2PI = 0.39894228040143267793994605993438;
double normpdf(double x, double u, double s) {
return (ONE_OVER_SQRT_2PI/s)*exp(-0.5*squared((x-u)/s));
}
Upvotes: 2