Reputation: 341
I have a vector of simple numbers such as:
a=[1 2 3 4 5 6 7 8]
I would like to have all the numbers of the vector that fall in between [25% 75%] quartiles. However, when I use the command below:
quantile(a,[0.25 0.75])
It only gives me 2 numbers of 2 and 6 (instead of 3,4,5,6). Do you have any solution how I can do it?
Upvotes: 3
Views: 1333
Reputation: 2854
Based on the mathematical definition of a quantile, the quantile()
function should not be returning {3,4,5,6}
given [0.25 0.75]
.
A quantile of a
may be thought of as the inverse of the cumulative distribution function (CDF) for a
. Since the CDF Fa(x) = P(a ≤ x) is a right-continuous increasing function, its inverse Fa-1(q) will be a one-to-one function as well.
Thus quantile(0.25)
can only return a single value (scalar), the smallest value x
such that P(a ≤ x) = 0.25.
However, logical indexing will do the trick. See code below.
% MATLAB R2017a
a = [1 2 3 4 5 6 7 8];
Q = quantile(a,[0.25 0.75]) % returns 25th & 75th quantiles of a
aQ = a(a>=Q(1) & a<=Q(2)) % returns elements of a between 25th & 75th quantiles (inclusive)
Upvotes: 8