xis a vector, then
ris the difference between the 75th and the 25th percentiles of the data contained in
xis a matrix, then
ris a row vector containing the difference between the 75th and the 25th percentiles of the sample data in each column of
xis a multidimensional array, then
iqroperates along the first nonsingleton dimension of
x. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same.
Compute the Interquartile Range
Generate a 4-by-4 matrix of random data from a normal distribution with parameter values equal to 10 and 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 Interquartile Range of Multidimensional Array
Compute the interquartile range of a multidimensional array over multiple dimensions by specifying the
vecdim input arguments.
Create a 3-by-4-by-2 array
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
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
rpage(1,1,1) is the interquartile range of all the elements in
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
rrow(3) is the interquartile range of all the elements in
Compute the Normal Distribution Interquartile Range
Create a standard normal distribution object with the mean, , equal to 0 and the standard deviation, , 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
Interquartile Range of a Fitted Distribution
Load the sample data. Create a vector containing the first column of students’ exam grade data.
load examgrades; x = grades(:,1);
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.
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
ans = 11.7634
boxplot to visualize the interquartile range.
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.
x — Input array
vector | matrix | multidimensional array
Input array, specified as a vector, matrix, or multidimensional array.
dim — Dimension
positive integer value
Dimension along which the interquartile range is calculated, specified as
a positive integer. For example, for a matrix
dim is equal to 1,
the interquartile range for the columns of
dim is equal to 2,
the interquartile range for the rows of x. For
along the first nonsingleton dimension of
vecdim — Vector of dimensions
positive integer vector
Vector of dimensions, specified as a positive integer vector. Each element
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
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
r — Interquartile range values
scalar | vector | matrix | multidimensional array
Interquartile range values, returned as a scalar, vector, matrix, or multidimensional array.
If you input an array
x, then the dimensions of
rdepend on whether the
vecdiminput arguments are specified. Each interquartile range value in
ris the difference between the 75th and the 25th percentiles of the specified data contained in
If you input a probability distribution
pd, then the scalar value of
ris the difference between the values of the 75th and 25th percentiles of the probability distribution.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
These input arguments are not supported:
diminput argument must be a compile-time constant.
If you do not specify the
diminput argument, the working (or operating) dimension can be different in the generated code. As a result, run-time errors can occur. For more details, see Automatic dimension restriction (MATLAB Coder).
The input argument
pdcan be a fitted probability distribution object for beta, exponential, extreme value, lognormal, normal, and Weibull distributions. Create
pdby fitting a probability distribution to sample data from the
fitdistfunction. For an example, see Code Generation for Probability Distribution Objects.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
The input arguments
vecdimare not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).