Interquartile range of data set
interquartile range values of elements in input data
r = iqr(
Ais a vector, then
ris the difference between the 75th and the 25th percentiles of the data contained in
Ais 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
Ais a multidimensional array, then
rcontains the interquartile range values computed along the first array dimension of size greater than 1. The size of this dimension is 1 while the sizes of all other dimensions remain the same as the input data.
Interquartile Range of Matrix Columns or Rows
Generate a 4-by-4 matrix of normally distributed random data.
rng default % for reproducibility A = randn(4,4)
A = 4×4 0.5377 0.3188 3.5784 0.7254 1.8339 -1.3077 2.7694 -0.0631 -2.2588 -0.4336 -1.3499 0.7147 0.8622 0.3426 3.0349 -0.2050
Compute the interquartile range for each column of data.
r = iqr(A)
r = 1×4 2.2086 1.2013 2.5969 0.8541
Compute the interquartile range for each row of data.
r2 = iqr(A,2)
r2 = 4×1 1.7237 2.9870 1.9449 1.8797
Interquartile Range of Multidimensional Array
Compute the interquartile range of a multidimensional array over multiple dimensions by specifying the "
Create a 3-by-4-by-2 array.
A = reshape(1:24,[3 4 2])
A = A(:,:,1) = 1 4 7 10 2 5 8 11 3 6 9 12 A(:,:,2) = 13 16 19 22 14 17 20 23 15 18 21 24
Compute the interquartile range of all the values in
rall = iqr(A,"all")
rall = 12
Compute the interquartile range of each page of
A. Specify the first and second dimensions as the operating dimensions along which the interquartile range is calculated.
rPage = iqr(A,[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
A(i,:,:) slice by specifying the second and third dimensions as the operating dimensions.
rRow = iqr(A,[2 3])
rRow = 3×1 12 12 12
rRow(3) is the interquartile range of all the elements in
A — Input array
vector | matrix | multidimensional array
Input array, specified as a vector, matrix, or multidimensional array.
dim — Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.
Consider an input matrix
r = iqr(A,1)computes the interquartile range of the columns in
A. Because 1 is the specified operating dimension,
rhas a number of rows equal to the number of columns in
r = iqr(A,2)computes the interquartile range of the rows in
A. Because 2 is the specified operating dimension,
rhas a number of columns equal to the number of rows in
dim indicates the dimension of
whose length reduces to 1. The
size(r,dim) is 1, while the sizes of
all other dimensions of output
r remain the same as the input
vecdim — Vector of dimensions to operate along
vector of positive integers
Vector of dimensions to operate along, specified as a vector of positive integers. Each element represents a dimension of the input data.
The size of the output
r in the specified operating dimensions is
1. The length of
r in all other dimensions remains the same as the
Consider a 2-by-3-by-3 input array,
2]) returns a 1-by-1-by-3 array because 1 and 2 are the operating
dimensions. Each page of the output array contains the interquartile range of the
elements on the corresponding page of
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
vecdiminputs 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).
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version HistoryIntroduced before R2006a
R2022b: Improved performance with small input data
iqr function shows improved performance due to faster input
parsing. The performance improvement is most significant when input parsing is a greater
portion of the computation time. This situation occurs when:
The size of the input data is small.
Computation is along the default operating dimension.
For example, this code calculates the interquartile range of a 4000-element matrix. The code is about 4.67x faster than in the previous release.
function timingIqr A = rand(400,10); for k = 1:3e3 r = iqr(A); end end
The approximate execution times are:
R2022a: 0.98 s
R2022b: 0.21 s
The code was timed on a Windows® 10, Intel®
Xeon® CPU E5-1650 v4 @ 3.60 GHz test system using the
R2022a: Moved to MATLAB from Statistics and Machine Learning Toolbox
iqr required Statistics and Machine Learning Toolbox™.