How to pick the j-th percentile of a vector?

9 ビュー (過去 30 日間)
MRC
MRC 2014 年 5 月 2 日
コメント済み: Siddhartha 2016 年 4 月 7 日
Hi, I have a matrix A nx1, e.g.
A=randn(200,1);
II want to pick the element of A which is the 25th percentile above the minimum in A. How can I do it?
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 2 日
What does that mean?
Siddhartha
Siddhartha 2016 年 4 月 7 日
function val = SpecialPercentile(arr, pct)
len = length(arr);
ind = floor(pct/100*len);
newarr = sort(arr);
val = newarr(ind);
end
Then call this function p = SpecialPercentile(A, 25);

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2014 年 5 月 2 日
If you don’t have the Statistics Toolbox, this doesn’t replicate the prctile results exactly, but it’s close:
pctl = @(v,p) interp1(linspace(0.5/length(v), 1-0.5/length(v), length(v))', sort(v), p*0.01, 'spline');
where v is the data vector and p is the percentile. You would call it as:
p = pctl(A, 25);
in your example.

その他の回答 (2 件)

Justin
Justin 2014 年 5 月 2 日
編集済み: Justin 2014 年 5 月 2 日
Is the function prctile what you are looking for?
It is in the statistics toolbox. You can use it to find the specific percentile you are looking for (in this case 25) and then find the minimum element in A greater than the percentile number.
Let me know if this makes sense or if you would like an example.

Image Analyst
Image Analyst 2014 年 5 月 2 日
Do you mean like this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 30;
A=randn(200,1);
sortedA = sort(A)
minA = min(A) % Just for information - not used
% Get cumulative distribution function
cdf = cumsum(sortedA - sortedA(1))
bar(cdf);
% Normalize
normalizedCdf = cdf / cdf(end)
% Plot it.
plot(sortedA,normalizedCdf, 'LineWidth', 2); % Show in plot.
grid on;
title('Cumulative Distribution Function', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Find index where it exceeds 25% for the first time
indexOf25Percentile = find(normalizedCdf > 0.25, 1, 'first')
% Find value where it exceeds 25% for the first time
valueOf25Percentile = sortedA(indexOf25Percentile)
% Plot vertical bar there
line([valueOf25Percentile, valueOf25Percentile], [0, .25],...
'Color', 'r', 'LineWidth', 2);
% Plot horizontal bar there
xl = xlim;
line([xl(1), valueOf25Percentile], [0.25, .25],...
'Color', 'r', 'LineWidth', 2);
message = sprintf('25 Percentile happens at %f (index %d)',...
valueOf25Percentile, indexOf25Percentile);
uiwait(msgbox(message));
  4 件のコメント
MRC
MRC 2014 年 5 月 2 日
Well, I have just applied this definition http://en.wikipedia.org/wiki/Percentile section nearest rank
I'm not sure why your value should be different
Image Analyst
Image Analyst 2014 年 5 月 2 日
You can do it that way if you want. It's like I'm taking the rank of the y values and you're taking the rand of the x values. Notice on the red lines that my 25% is 25% of the y (which happens at an x of -0.17), and yours would be the 25% of the x (-2.25) and you'd read off the y that you get at x = -2.25 (which is like 0.01 or something). If the cdf is linear, like you'd get with a uniform distribution, then they'll give the same value. If not, then they'll be different.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by