MATLAB: Obtaining a list of percentages/cumulative probabilities from a data vector given the percentile/quantile values

MATLAB

The "prctile" and "quantile" functions compute percentile and quantile values from a given data vector. How can I perform the inverse operation, i.e. provide a list of values and get the corresponding percentages/cumulative probabilities?

Best Answer

While MATLAB does not include an inbuilt function to accomplish this, it is possible to write some simple code to do so. The following page of the MATLAB documentation provides details of the algorithm used by the "prctile" and "quantile" functions.
The following code can be used to reverse these algorithms to arrive at the percentage and cumulative probability values given a data vector "X" and a list of percentile or quantile values "Y".
For "prctile",
 
>> sortedX = sort(X);
>> listPercentages = (100/length(X))*(0.5:1:length(X)-0.5);
>> percentages = interp1(sortedX,listPercentages,Y);
 
For "quantile",
 
>> sortedX = sort(X);
>> listCumProb = (1/length(X))*(0.5:1:length(X)-0.5);
>> cumProbs = interp1(sortedX,listCumProb,Y);