prctile function vs excel percentile
Mostra commenti meno recenti
Hi,
when I run the below,
A = [ 0.02; 0.01 ; 0.01; 0.01 ; 0.04 ]; B = prctile(A,99.95);
I get B = 0.0400, which seems rounded.
In excel the same percentile function produces a result of 0.03996
Is there a way to show a less rounded result in matlab? Any other function or calculation in matlab e.g. 65/43, will produce a result (1.5116) that doesn't round the decimals like prctile does.
(even if I try "format long" the result will still be 0.040000000...)
Many thanks
Risposta accettata
Più risposte (2)
Chan Dennis
il 23 Dic 2018
Clearly, both percentile alogrithms are the different cases of the linear interpolation between closest ranks method. More details in Percentile - Wikipedia.
- When let C=0.5, the result is equal prctile function in matlab.
- When let C=1, the result is equal excel percentile.
There are two matlab functions below, which are the implementations of the above algorithms.
prctile_one.m (resuls equal excel PERCENTILE and PERCENTILE.INC)
function V_x = prctile_one(V,p)
% The linear interpolation between closest ranks method in the case of `C=1`
% [Percentile - Wikipedia](https://en.wikipedia.org/wiki/Percentile)
if ~isvector(p) || numel(p) == 0 || any(p < 0 | p > 1) || ~isreal(p)
error('Make sure the Second digit within the [0,1] interval');
end
V = sort(V,'ascend');
N = length(V);
x = p*(N-1)+1; % position x
if floor(x) < N
V_x = V(floor(x)) + mod(x,1)*(V(floor(x)+1) - V(floor(x))); % value
else
V_x = V(N); % position N
end
prctile_half.m (resuls equal matlab prctile)
function V_x = prctile_half(V,p)
% The linear interpolation between closest ranks method in the case of `C=0.5`
% [Percentile - Wikipedia](https://en.wikipedia.org/wiki/Percentile)
if ~isvector(p) || numel(p) == 0 || any(p < 0 | p > 1) || ~isreal(p)
error('Make sure the Second digit within the [0,1] interval');
end
V = sort(V,'ascend');
N = length(V);
p_1 = 1/(2*N); % position 1
p_N = 1 - 1/(2*N); % position N
if p_1<=p && p<=p_N
x = N*p + 0.5; % position x
V_x = V(floor(x)) + mod(x,1)*(V(floor(x)+1) - V(floor(x))); % value
else if 0<=p && p<p_1
V_x = V(1); % value 1
else if p_N<p && p<=1
V_x = V(N); % value N
end
end
end
1 Commento
John D'Errico
il 24 Mar 2015
Personally, I might have chosen the Excel scheme. The highest datapoint seems like it should be 100%, the smallest, 0%. But I can see entirely valid arguments for doing it other ways.
Of course, were I truly the author of the code, I would have offered multiple schemes, and let the user pick among them as options. But then I tend to overdo those things as an author.
Categorie
Scopri di più su Descriptive Statistics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!