MATLAB: Percentile of a value based on array of data

MATLABpercentilerank

Hello!
I have an array with historical data and would like to know where an exogenous variable fits in this array using percentile.
Let's assume a vector with 1:1000 and 950 as a given, the function should return 95%.
It would be possible to create a function to do the task, but maybe matlab has a built-in function to do this ?
Thanks a lot, Max.

Best Answer

% Test data
historicalData = rand(1000, 1);
exogenousVariable = 0.7;
% Compute centile
nless = sum(historicalData < exogenousVariable);
nequal = sum(historicalData == exogenousVariable);
centile = 100 * (nless + 0.5*nequal) / length(historicalData);
This actually computes 94.95 rather than 95 for your example, because the test value is equal to one of the data points. You can round to the nearest whole number or to any other number of significant figures using the round function.