# error in prctile function?

6 views (last 30 days)
Jim Hokanson on 24 Nov 2022
Answered: Jonas on 24 Nov 2022
I'm creating my own percentile function and wanted to compare it to the prctile function available in MATLAB.
I am pretty sure I am doing things right but I am getting different values than prctile.
data = [1 2 3 4 5];
p = 0:100;
y1 = prctile(data,p);
y2 = my_function(data,p);
My values are linear (1,1.04,1.08,etc.) as one might expect but the values from MATLAB's prctile are not. What gives?
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Jonas on 24 Nov 2022
did you read the documentation? there is an example given for the interpolation and how the given numbers may be distributed. You should also ask yourself, what the 0% border (which contains no data point of the given data) is.
So Matlab thinks you have to have at least one data point inside the smallest percentage. So at least 1 data point, which equals 20% when you have 5 data points. In addition, Matlab centers the given Data, so 1:5 are not percentile 20:20:100 but 10:20:90. If you ask for percentile bigger/smaller that range, Matlab rounds it to the smallest/greatest acceptable percentiles (here 10% and 90%)
data = [1 2 3 4 5];
y1 = prctile(data,10:20:90)
y1 = 1×5
1 2 3 4 5
y1 = prctile(data,20:20:100) % you can see that matlab interpolates and caps at the same time
y1 = 1×5
1.5000 2.5000 3.5000 4.5000 5.0000
y1 = prctile(data,[0 10:20:90 100]) % here a similar example
y1 = 1×7
1 1 2 3 4 5 5
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Noncentral t Distribution in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by