Quantcast

Documentation Center

  • Trial Software
  • Product Updates

quantile

Quantiles of a data set

Syntax

Description

example

Y = quantile(X,p) returns quantiles of the values in data vector or matrix X for the cumulative probability or probabilities p in the interval [0,1].

  • If X is a vector, then Y is a scalar or a vector having the same length as p.

  • If X is a matrix, then Y is a row vector or a matrix where the number of rows of Y is equal to the length of p.

  • For multidimensional arrays, quantile operates along the first nonsingleton dimension of X.

example

Y = quantile(X,p,dim) returns quantiles along dimension dim.

example

Y = quantile(X,N) returns quantiles for N evenly spaced cumulative probabilities (1/(N + 1), 2/(N + 1), ..., N/(N + 1)) for integer N>1.

  • If X is a vector, then Y is a scalar or a vector with length N.

  • If X is a matrix, then Y is a matrix where the number of rows of Y is equal to N.

  • For multidimensional arrays, quantile operates along the first nonsingleton dimension of X.

example

Y = quantile(X,N,dim) returns quantiles at the N evenly-spaced cumulative probabilities (1/(N+1), 2/(N+1), ..., N/(N+1)) for integer N>1 along dimension dim.

Examples

expand all

Quantiles for Given Probabilities

Calculate the quantiles of a data set for specified probabilities.

Generate a data set of size 10.

rng('default'); % for reproducibility
x = normrnd(0,1,1,10)
x =
    0.5377    1.8339   -2.2588    0.8622    0.3188   -1.3077   -0.4336    0.3426    3.5784    2.7694

Calculate the 0.3 quantile.

y = quantile(x,0.30) 
y =
   -0.0574

Calculate the quantiles for the cumulative probabilities 0.025, 0.25, 0.5, 0.75, and 0.975.

y = quantile(x,[0.025 0.25 0.50 0.75 0.975]) 
y =
   -2.2588   -0.4336    0.4401    1.8339    3.5784

Quantiles of a Matrix for Given Probabilities

Calculate the quantiles along the columns and rows of a data matrix for specified probabilities.

Generate a 4-by-6 data matrix.

rng('default'); % for reproducibility
X = normrnd(0,1,4,6)
X =
    0.5377    0.3188    3.5784    0.7254   -0.1241    0.6715
    1.8339   -1.3077    2.7694   -0.0631    1.4897   -1.2075
   -2.2588   -0.4336   -1.3499    0.7147    1.4090    0.7172
    0.8622    0.3426    3.0349   -0.2050    1.4172    1.6302

Calculate the 0.3 quantile for each column of X (dim = 1).

y = quantile(x,0.3,1) 
y =
   -0.3013   -0.6958    1.5336   -0.1056    0.9491    0.1078

quantile returns a row vector y when calculating one quantile for each column of a matrix. For example, -0.3013 is the 0.3 quantile of the first column of X with elements (0.5377, 1.8339, -2.2588, 0.8622). y = quantile(X,0.3) returns the same answer because the default value of dim is 1.

Calculate the 0.3 quantile for each row of X (dim = 2).

y = quantile(x,0.3,2)
y =
    0.3844
   -0.8642
   -1.0750
    0.4985

quantile returns a column vector y when calculating one quantile for each row of a matrix. For example 0.3844 is the 0.3 quantile of the first row of X with elements (0.5377, 0.3188, 3.5784, 0.7254, -0.1241, 0.6715).

Quantiles for N Evenly Spaced Cumulative Probabilities

Calculate the quantiles of a data set for a given number of quantiles.

Generate a data set of size 10.

rng('default'); % for reproducibility
x = normrnd(0,1,1,10)
x =
    0.5377    1.8339   -2.2588    0.8622    0.3188   -1.3077   -0.4336    0.3426    3.5784    2.7694

Calculate four evenly spaced quantiles.

y = quantile(x,4)
y =
   -0.8706    0.3307    0.6999    2.3017

Using y = quantile(x,[0.2,0.4,0.6,0.8]) is another way to return the four evenly spaced quantiles.

Quantiles of a Matrix for Given Number of Quantiles

Calculate the N evenly spaced quantiles along the columns and rows of a data matrix.

Generate a 6-by-10 data matrix.

rng('default');  % for reproducibility
X = unidrnd(10,6,7)
X =

     9     3    10     8     7     8     7
    10     6     5    10     8     1     4
     2    10     9     7     8     3    10
    10    10     2     1     4     1     1
     7     2     5     9     7     1     5
     1    10    10    10     2     9     4

Calculate three evenly spaced quantiles for each column of X (dim = 1).

y = quantile(X,3,1) 
y =

    2.0000    3.0000    5.0000    7.0000    4.0000    1.0000    4.0000
    8.0000    8.0000    7.0000    8.5000    7.0000    2.0000    4.5000
   10.0000   10.0000   10.0000   10.0000    8.0000    8.0000    7.0000

Each column of matrix y corresponds to the three evenly spaced quantiles of each column of matrix X. For example, the first column of y with elements (2, 8, 10) has the quantiles for the first column of X with elements (9, 10, 2, 10, 7, 1). y = quantile(X,3) returns the same answer because the default value of dim is 1.

Calculate three evenly spaced quantiles for each row of X (dim = 2).

y = quantile(X,3,2)
y =

    7.0000    8.0000    8.7500
    4.2500    6.0000    9.5000
    4.0000    8.0000    9.7500
    1.0000    2.0000    8.5000
    2.7500    5.0000    7.0000
    2.5000    9.0000   10.0000

Each row of matrix y corresponds to the three evenly spaced quantiles of each row of matrix X. For example, the first row of y with elements (7, 8, 8.75) has the quantiles for the first column of X with elements (9, 3, 10, 8, 7, 8, 7).

Median and Quartiles for Even Number of Data Elements

Find median and quartiles of a vector, x, with even number of elements.

Enter the data.

x = [2 5 6 10 11 13]
x =
     2     5     6    10    11    13

Calculate the median of x.

y = quantile(x,0.50) 
y =
     8

Calculate the quartiles of x.

y = quantile(x,[0.25, 0.5, 0.75]) 
y =
     5     8    11

Using y = quantile(x,3) is another way to compute the quartiles of x.

These results might be different than the textbook definitions because quantile uses linear interpolation to find the median and quartiles.

Median and Quartiles for Odd Number of Data Elements

Find median and quartiles of a vector, x, with odd number of elements.

Enter the data.

x = [2 4 6 8 10 12 14]
x =

     2     4     6     8    10    12    14

Find the median of x.

y = quantile(x,0.50)
y =
     8

Find the quartiles of x.

y = quantile(x,[0.25, 0.5, 0.75])
y =
    4.5000    8.0000   11.5000

Using y = quantile(x,3) is another way to compute the quartiles of x.

These results might be different than the textbook definitions because quantile uses linear interpolation to find the median and quartiles.

Input Arguments

expand all

X — Input datavector | array

Input data, specified as a vector or array.

Data Types: double | single

p — Cumulative probabilitiesscalar | vector

Cumulative probabilities, for which to compute the quantiles, specified as a scalar or vector of scalars from 0 to 1.

Example: 0.3

Example: [0.25, 0.5, 0.75]

Example: (0:0.25:1)

Data Types: double | single

N — Number of quantilespositive integer

Number of quantiles to compute, specified as a positive integer. quantile returns N quantiles that divide the data set into evenly distributed N+1 segments.

Data Types: double | single

dim — Dimension 1 (default) | positive integer

Dimension along which the quantiles of a matrix X are required, specified as a positive integer. For example, for a matrix X, when dim = 1, quantile returns the quantile(s) of the columns of X and when dim = 2, quantile returns the quantile(s) of the rows of X. For a multidimensional array X, the length of the dimth dimension of Y is same as length of p.

Output Arguments

expand all

Y — Quantilesscalar | array

Quantiles of a data vector or matrix, returned as a scalar or array for one or multiple values of cumulative probabilities.

  • If X is a vector, then Y is a scalar or a vector with the same length as the number of quantiles required (N or length(p)). Y(i) contains the p(i) quantile.

  • If X is a matrix, then Y is a vector or a matrix with the length of dimth dimension equal to the number of quantiles required (N or length(p)). When dim = 1, for example, the ith row of Y contains the p(i) quantiles of columns of X.

  • If X is an array of dimension d, then Y is an array with the length of dimth dimension equal to the number of quantiles required (N or length(p)).

More About

expand all

Multidimensional Array

A multidimensional array is an array with more than two dimensions. For example, if X is a 1-by-3-by-4 array, then X is a 3-D array.

First Nonsingleton Dimension

A first nonsingleton dimension is the first dimension of an array whose size is not equal to 1. For example, if X is a 1-by-2-by-3-by-4 array, then the second dimension is the first nonsingleton dimension of X.

Linear Interpolation

Linear interpolation uses linear polynomials to find yi = f(xi), the values of the underlying function Y = f(X) at the points in the vector or array x. Given the data points (x1, y1) and (x2, y2), where y1 = f(x1) and y2 = f(x2), linear interpolation finds y = f(x) for a given x between x1 and x2 as follows:

Similarly, if the 1.5/n quantile is y1.5/n and the 2.5/n quantile is y2.5/n, then linear interpolation finds the 2.3/n quantile y2.3/n as

Algorithms

For an n-element vector X, quantile computes quantiles as follows:

  1. The sorted values in X are taken as the (0.5/n), (1.5/n), ..., ([n – 0.5]/n) quantiles. For example:

    • For a data vector of five elements such as {6, 3, 2, 10, 1}, the sorted elements {1, 2, 3, 6, 10} respectively correspond to the 0.1, 0.3, 0.5, 0.7, 0.9 quantiles.

    • For a data vector of six elements such as {6, 3, 2, 10, 8, 1}, the sorted elements {1, 2, 3, 6, 8, 10} respectively correspond to the (0.5/6), (1.5/6), (2.5/6), (3.5/6), (4.5/6), (5.5/6) quantiles.

  2. quantile uses Linear interpolation to compute quantiles for probabilities between (0.5/n) and ([n – 0.5]/n).

  3. For the quantiles corresponding to the probabilities outside that range, quantile assigns the minimum or maximum values in X.

quantile treats NaNs as missing values and removes them.

See Also

| |

Was this topic helpful?