Categories

# 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:

1. 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].
2. 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:

1. Use normrnd function to get 12 random normally distributed numbers
2. 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)`[Initializing 12 normally distributed random numbers]

`A = quantile (nr, 0.40)`[Passing the numbers generated above as input to the quantile function to get the 0.4 quantile]

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

## 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:

1. Use the normrnd function to get random normally distributed elements for the matrix.
2. 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.’
3. 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)`[Initializing a 3 x 3 matrix with normally distributed elements]

`A = quantile (nr, 0.40, 1)`[Passing the matrix created above as input to the quantile function to get the 0.4 quantile. Please note that we have passed ‘1’ as the 3rd 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:

## 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)`[Passing the matrix created above as input to the quantile function to get the 0.4 quantile. Please note that we have passed ‘2’ as the 3rd 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:

## 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)`[Initializing 8 normally distributed random numbers]

`A = quantile (nr, 3)`[Passing the numbers generated above as input to the quantile function to get 3 evenly distant quantiles]

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

## Output:

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