Updated March 6, 2023

## Introduction t0 Matlab quantile

Quantile function is used in MATLAB to divide a sample into adjacent, equal-sized subgroups. Quantile is also referred to as ‘fractile’, and in statistics, it is used to divide a given probability distribution into small areas which have equal probability.

The median in statistics, for example, is a quantile which is placed inside a probability distribution such that half of the data is less than it and the other half is more than it. So, it divides a probability distribution into 2 equal areas and is called a 2-quantile.

**Syntax of the quantile function:**

`A = quantile (nr, prob)`

`A = quantile (nr, X)`

**Details of the quantile function:**

- A = quantile (nr, prob) is used to return quantiles for the elements present in a vector or array for the probability ‘prob’, which lies in the range [0,1].
- A = quantile (nr, X) is used to return the quantiles for ‘X’ equally placed cumulative probabilities. Mathematically, this is given by (1 / (X + 1), 2 / (X + 1), …, X / (X + 1)) for integer X > 1

### Examples of Matlab quantile

Let us now understand how to use the quantile function in MATLAB.

#### Example #1

This example will use the quantile function to find a quantile for 12 normally distributed numbers. We will use the ‘normrnd’ function of MATLAB to get these normally distributed numbers. Below are the steps to be followed:

- Use normrnd function to get 12 random normally distributed numbers
- Pass these numbers to the quantile function to get the quantile. For this example, we will calculate the 0.4 quantile

**Code:**

`nr = normrnd (0, 1, 1, 12)`

`A = quantile (nr, 0.40)`

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained 0.4 quantile for our 12 normally distributed random numbers. This means that 40% of our numbers are below -0.0987, and the rest 60% are above it.

#### Example #2

In this example, we will use the quantile function to find quantiles for a matrix. We will use a 3 x 3 matrix of probabilities and will calculate quantiles, first for its columns and then for its rows. We will use the ‘normrnd’ function of MATLAB to get these normally distributed numbers as the elements of the matrix. Below are the steps to be followed:

- Use the normrnd function to get random normally distributed elements for the matrix.
- Pass these numbers to the quantile function to get the quantiles for columns. For this, we will have to pass the ‘dim’ argument as 1, which represents ‘columns.’
- Pass these numbers to the quantile function to get the quantiles for rows. For this, we will have to pass the ‘dim’ argument as 2, which represents ‘rows.’

**Code to calculate quantile for columns:**

`nr = normrnd (0, 1, 3, 3)`

`A = quantile (nr, 0.40, 1)`

^{rd}argument, which represents that the calculation is to be performed along the column]

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained 0.4 quantile for the columns of our 3 x 3 matrix of normally distributed random numbers.

**Code to calculate quantile for rows:**

`A = quantile (nr, 0.40, 2)`

^{rd}argument, which represents that the calculation is to be performed along the row]

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained 0.4 quantile for the rows of our 3 x 3 matrix of normally distributed random numbers.

#### Example #3

In this example, we will use the quantile function to calculate evenly distant quantiles. We will use the ‘normrnd’ function of MATLAB to get 8 normally distributed numbers.

**Code:**

`nr = normrnd (0, 1, 1, 8)`

`A = quantile (nr, 3)`

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained 3 evenly distant quantiles for our 8 normally distributed random numbers.

### Conclusion

- Quantile function is used in MATLAB to divide a distribution into smaller parts.
- An ‘x%’ quantile means that ‘x%’ of numbers are below this quantile, and the rest are above it.
- Quantile function can be used to find quantiles for both arrays and matrices.

### Recommended Articles

This is a guide to Matlab quantile. Here we discuss how to use the quantile function in MATLAB along with the examples and outputs. You may also have a look at the following articles to learn more –