user366312
user366312

Reputation: 16988

What are the differences between different gaussian functions in Matlab?

  1. y = gauss(x,s,m)
  2. Y = normpdf(X,mu,sigma)
  3. R = normrnd(mu,sigma)

What are the basic differences between these three functions?

Upvotes: 5

Views: 2076

Answers (2)

Leander Moesinger
Leander Moesinger

Reputation: 2462

Y = normpdf(X,mu,sigma) is the probability density function for a normal distribution with mean mu and stdev sigma. Use this if you want to know the relative likelihood at a point X.

R = normrnd(mu,sigma) takes random samples from the same distribution as above. So use this function if you want to simulate something based on the normal distribution.

y = gauss(x,s,m) at first glance looks like the exact same function as normpdf(). But there is a slight difference: Its calculation is

Y = EXP(-(X-M).^2./S.^2)./(sqrt(2*pi).*S)

while normpdf() uses

Y = EXP(-(X-M).^2./(2*S.^2))./(sqrt(2*pi).*S)

This means that the integral of gauss() from -inf to inf is 1/sqrt(2). Therefore it isn't a legit PDF and I have no clue where one could use something like this.

For completeness we also have to mention p = normcdf(x,mu,sigma). This is the normal cumulative distribution function. It gives the probability that a value is between -inf and x.

Upvotes: 7

EBH
EBH

Reputation: 10450

A few more insights to add to Leander good answer:

When comparing between functions it is good to look at their source or toolbox. gauss is not a function written by Mathworks, so it may be redundant to a function that comes with Matlab.

Also, both normpdf and normrnd are part of the Statistics and Machine Learning Toolbox so users without it cannot use them. However, generating random numbers from a normal distribution is quite a common task, so it should be accessible for users that have only the core Matlab. Hence, there is a redundant function to normrnd which is randn that is part of the core Matlab.

Upvotes: 5

Related Questions