Main Content

iqr

Interquartile range of data set

Description

r = iqr(A) returns the interquartile range values of elements in input data A.

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

  • If A 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 A.

  • If A is a multidimensional array, then r contains the interquartile range values computed along the first array dimension whose size does not equal 1. The size of r along this dimension is 1, while the sizes of all other dimensions remain the same as the input data.

example

r = iqr(A,"all") returns the interquartile range values of all the elements in A.

example

r = iqr(A,dim) operates along the dimension dim. For example, if A is a matrix, then iqr(A,2) operates on the elements in each row.

example

r = iqr(A,vecdim) operates along the dimensions specified in the vector vecdim. For example, if A is a matrix, then iqr(A,[1 2]) operates on all the elements of A because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

[r,q] = iqr(___) also returns the first and third quartiles of elements in input data A for any of the previous syntaxes. The first quartile is the 25th percentile of the input data, and the third quartile is the 75th percentile. (since R2024a)

example

Examples

collapse all

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

Compute the interquartile range of a multidimensional array over multiple dimensions by specifying the "all" or vecdim input.

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 A.

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 A(:,:,1).

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(3,:,:).

Since R2024a

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 first and third quartiles in addition to the interquartile range for each column of data. The first element in each column of q is the first quartile of the input data, and the second element is the third quartile.

[r,q] = iqr(A)
r = 1×4

    2.2086    1.2013    2.5969    0.8541

q = 2×4

   -0.8606   -0.8706    0.7098   -0.1340
    1.3480    0.3307    3.3067    0.7201

Input Arguments

collapse all

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

Data Types: double | single | duration

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 whose size does not equal 1.

Consider an input matrix A:

  • r = iqr(A,1) computes the interquartile range of the columns in A. Because 1 is the specified operating dimension, r has a number of rows equal to the number of columns in A.

  • r = iqr(A,2) computes the interquartile range of the rows in A. Because 2 is the specified operating dimension, r has a number of columns equal to the number of rows in A.

Dimension dim indicates the dimension of r 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 data.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

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 input data.

Consider a 2-by-3-by-3 input array, A. iqr(A,[1 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 A.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

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

Since R2024a

First and third quartiles, returned as a vector, matrix, or multidimensional array.

q and r are the same size, expect the size of q along the smallest operating dimension is 2. Along that dimension, the first element is the first quartile of the input data and the second element is the third quartile.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

| | |