# The mvnpdf() Function in MATLAB

This tutorial will discuss evaluating the probability density function of a multivariate normal distribution using the `mvnpdf()` function in MATLAB.

## Use the `mvnpdf()` Function in MATLAB

The multivariate normal distribution of the generalized version of the univariate normal distribution to two or more variables. The multivariate normal distribution has two parameters similar to the univariate normal distribution, including the covariance matrix and the mean vector.

The off-diagonal elements of the covariance matrix contain the covariances between variables, and the diagonal elements of the covariance matrix contain the variances for each variable. In MATLAB, we use the `mvnpdf()` function to find the probability density function of the multivariate normal distribution.

The `mvnpdf()` function has three syntaxes given below.

Syntax:

``````output = mvnpdf(input)
output = mvnpdf(input, mu)
output = mvnpdf(input, mu, sigma)
``````

The `output = mvnpdf(input)` will return the probability density function as an `n-by-1` vector of the input matrix, containing the multivariate normal distribution of dimension m. The function will evaluate the output at each input matrix row along the dimension n-by-m.

The `output = mvnpdf(input, mu)` will return the probability density function according to the `mu` variable, which defines the mean value of the multivariate normal distribution. The third syntax will also include the variable `sigma`, which defines the covariance of the multivariate normal distribution.

In the `output = mvnpdf(input, mu, sigma)`, if we only want to specify the value for `sigma` and we want to use the default value of `mu`, we can pass an empty vector as the value for `mu` variable as the second argument and set the value of `sigma` as the third argument. We can use the `mvnrnd()` function to create a multivariate normal distribution matrix containing random numbers.

To create the random multivariate normal distribution, we have to pass the value of `mu` and `sigma` variables inside the `mvnrnd()` function. We can also set the number of rows of the output matrix by defining the number as the third argument inside the `mvnrnd()` function.

For example, let’s create a random matrix of multivariate normal distribution using the `mrnrnd()` function and then find its probability density function using the `mvnpdf()` function.

Example Code:

``````mu_v = zeros(1,4);
Sigma_m = eye(4);
rng('default')
random_mvn = mvnrnd(mu_v,Sigma_m,6)
out_pdf = mvnpdf(random_mvn)
``````

Output:

``````random_mvn =

0.5377   -0.4336    0.7254    1.4090
1.8339    0.3426   -0.0631    1.4172
-2.2588    3.5784    0.7147    0.6715
0.8622    2.7694   -0.2050   -1.2075
0.3188   -1.3499   -0.1241    0.7172
-1.3077    3.0349    1.4897    1.6302

out_pdf =

0.0057
0.0016
0.0000
0.0002
0.0074
0.0000
``````

We used the `zeros()` function to create the vector for the `mu` variable and the `eye()` function to create the matrix for the `sigma` variable, and the `rng()` function to control the random number generator, and we have set it to default for reproducibility. The above output shows the random matrix of multivariate normal distribution and the vector containing the probability density function values.

Suppose we want to find the probability density function at certain points according to a different mean value. We can define the mean values in the `mu` variable as the second argument inside the `mvnpdf()` function.

The `mu` matrix size should be the same as the input matrix size of multivariate normal distribution.

Suppose we want to find the probability density function according to a different covariance value at certain points. We can define the covariance values in the `sigma` variable as the third argument inside the `mvnpdf()` function.

The number of columns of the `sigma` matrix should be the same as the number of columns of the multivariate normal distribution input matrix. If we want to find the probability density function at the same points, we can repeat the same point inside the `mu` variable, and by default, the `mu` variable is a vector of zeros, and the `sigma` variable is a matrix of ones.

Check this link for more details about the `mvnpdf()` function, or this link for the `mvnrnd()` function.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - MATLAB Function

• Find the Derivative of a Function Handle in MATLAB
• Find K-Nearest Neighbors in MATLAB
• Use Kronecker Delta in MATLAB