Search code examples
arraysmatlabquartile

How to find the index value of the quartile values in an array using Matlab?


I have a vector of dimension 1x3000. I have found the percentile value using the percentile function in Matlab. But I am unable to find the index value of the quartile inside the vector.

y = rand(1,3000);
Q_2 = prctile(y,50);
Idx = find(y==Q_2);

Idx is returning an empty value. I should be able to get a value of the index containing the median value.


Solution

  • You can efficiently find the entry closest to the median (or an arbitrary q_2 for that matter) with:

    [~,Idx]=min(abs(q_2-y));
    

    As per help min, the value returned as Idx corresponds to the first element with the minimum value in the vector of differences.