iqr

Interquartile range

Description

example

r = iqr(x) returns the interquartile range of the values in x.

• If x is a vector, then r is the difference between the 75th and the 25th percentiles of the data contained in x.

• If x is a matrix, then r is a row vector containing the difference between the 75th and the 25th percentiles of the sample data in each column of x.

• If x is a multidimensional array, then iqr operates along the first nonsingleton dimension of x. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.

example

r = iqr(x,'all') returns the interquartile range of all the values in x.

example

r = iqr(x,dim) returns the interquartile range along the dimension of x specified by dim.

example

r = iqr(x,vecdim) returns the interquartile range over the dimensions specified by vecdim. For example, if x is a matrix, then iqr(x,[1 2]) is the interquartile range of all the elements of x because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

r = iqr(pd) returns the interquartile range of the probability distribution pd.

Examples

collapse all

Generate a 4-by-4 matrix of random data from a normal distribution with parameter values $\mu$ equal to 10 and $\sigma$ equal to 1.

rng default  % For reproducibility
x = normrnd(10,1,4)
x = 4×4

10.5377   10.3188   13.5784   10.7254
11.8339    8.6923   12.7694    9.9369
7.7412    9.5664    8.6501   10.7147
10.8622   10.3426   13.0349    9.7950

Compute the interquartile range for each column of data.

r = iqr(x)
r = 1×4

2.2086    1.2013    2.5969    0.8541

Compute the interquartile range for each row of data.

r2 = iqr(x,2)
r2 = 4×1

1.7237
2.9870
1.9449
1.8797

Compute the interquartile range of a multidimensional array over multiple dimensions by specifying the 'all' and vecdim input arguments.

Create a 3-by-4-by-2 array X.

X = reshape(1:24,[3 4 2])
X =
X(:,:,1) =

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

X(:,:,2) =

13    16    19    22
14    17    20    23
15    18    21    24

Compute the interquartile range of all the values in X.

rall = iqr(X,'all')
rall = 12

Compute the interquartile range of each page of X. Specify the first and second dimensions as the operating dimensions along which the interquartile range is calculated.

rpage = iqr(X,[1 2])
rpage =
rpage(:,:,1) =

6

rpage(:,:,2) =

6

For example, rpage(1,1,1) is the interquartile range of all the elements in X(:,:,1).

Compute the interquartile range of the elements in each X(i,:,:) slice by specifying the second and third dimensions as the operating dimensions.

rrow = iqr(X,[2 3])
rrow = 3×1

12
12
12

For example, rrow(3) is the interquartile range of all the elements in X(3,:,:).

Create a standard normal distribution object with the mean, $\mu$, equal to 0 and the standard deviation, $\sigma$, equal to 1.

pd = makedist('Normal','mu',0,'sigma',1);

Compute the interquartile range of the standard normal distribution.

r = iqr(pd)
r = 1.3490

The returned value is the difference between the 75th and the 25th percentile values for the distribution. This is equivalent to computing the difference between the inverse cumulative distribution function (icdf) values at the probabilities y equal to 0.75 and 0.25.

r2 = icdf(pd,0.75) - icdf(pd,0.25)
r2 = 1.3490

Load the sample data. Create a vector containing the first column of students’ exam grade data.

Create a normal distribution object by fitting it to the data.

pd = fitdist(x,'Normal')
pd =
NormalDistribution

Normal distribution
mu = 75.0083   [73.4321, 76.5846]
sigma =  8.7202   [7.7391, 9.98843]

Compute the interquartile range of the fitted distribution.

r = iqr(pd)
r = 11.7634

The returned result indicates that the difference between the 75th and 25th percentile of the students’ grades is 11.7634.

Use icdf to determine the 75th and 25th percentiles of the students’ grades.

y = icdf(pd,[0.25,0.75])
y = 1×2

69.1266   80.8900

Calculate the difference between the 75th and 25th percentiles. This yields the same result as iqr.

y(2)-y(1)
ans = 11.7634

Use boxplot to visualize the interquartile range.

boxplot(x) The top line of the box shows the 75th percentile, and the bottom line shows the 25th percentile. The center line shows the median, which is the 50th percentile.

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types: single | double

Dimension along which the interquartile range is calculated, specified as a positive integer. For example, for a matrix x, when dim is equal to 1, iqr returns the interquartile range for the columns of x. When dim is equal to 2, iqr returns the interquartile range for the rows of x. For n-dimensional arrays, iqr operates along the first nonsingleton dimension of x.

Data Types: single | double

Vector of dimensions, specified as a positive integer vector. Each element of vecdim represents a dimension of the input array x. The output r has length 1 in the specified operating dimensions. The other dimension lengths are the same for x and r.

For example, if x is a 2-by-3-by-3 array, then iqr(x,[1 2]) returns a 1-by-1-by-3 array. Each element of the output array is the interquartile range of the elements on the corresponding page of x. Data Types: single | double

Probability distribution, specified as one of the probability distribution objects in this table.

Distribution ObjectFunction or App to Create Probability Distribution Object
BinomialDistributionmakedist, fitdist, Distribution Fitter
BirnbaumSaundersDistributionmakedist, fitdist, Distribution Fitter
BurrDistributionmakedist, fitdist, Distribution Fitter
ExponentialDistributionmakedist, fitdist, Distribution Fitter
ExtremeValueDistributionmakedist, fitdist, Distribution Fitter
GeneralizedExtremeValueDistributionmakedist, fitdist, Distribution Fitter
GeneralizedParetoDistributionmakedist, fitdist, Distribution Fitter
HalfNormalDistributionmakedist, fitdist, Distribution Fitter
InverseGaussianDistributionmakedist, fitdist, Distribution Fitter
KernelDistributionfitdist, Distribution Fitter
LogisticDistributionmakedist, fitdist, Distribution Fitter
LoglogisticDistributionmakedist, fitdist, Distribution Fitter
LognormalDistributionmakedist, fitdist, Distribution Fitter
LoguniformDistributionmakedist
MultinomialDistributionmakedist
NakagamiDistributionmakedist, fitdist, Distribution Fitter
NegativeBinomialDistributionmakedist, fitdist, Distribution Fitter
NormalDistributionmakedist, fitdist, Distribution Fitter
PiecewiseLinearDistributionmakedist
PoissonDistributionmakedist, fitdist, Distribution Fitter
RayleighDistributionmakedist, fitdist, Distribution Fitter
RicianDistributionmakedist, fitdist, Distribution Fitter
StableDistributionmakedist, fitdist, Distribution Fitter
tLocationScaleDistributionmakedist, fitdist, Distribution Fitter
TriangularDistributionmakedist
UniformDistributionmakedist
WeibullDistributionmakedist, fitdist, Distribution Fitter

Output Arguments

collapse all

Interquartile range values, returned as a scalar, vector, matrix, or multidimensional array.

• If you input an array x, then the dimensions of r depend on whether the 'all', dim, or vecdim input arguments are specified. Each interquartile range value in r is the difference between the 75th and the 25th percentiles of the specified data contained in x.

• If you input a probability distribution pd, then the scalar value of r is the difference between the values of the 75th and 25th percentiles of the probability distribution.