116 views (last 30 days)

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?

Star Strider
on 2 May 2014

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.

Justin
on 2 May 2014

Edited: Justin
on 2 May 2014

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
on 2 May 2014

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));

Image Analyst
on 2 May 2014

What do you mean by percentile? I mean like the percentile you'd get if you took the histogram and cdf, so it comes from the frequency of occurrence. Yours goes based strictly on the number of elements and ignores what the values are, which seems strange. So you're always taking the 50th element regardless of what that element's value is or how the values are distributed. That's not the definition of percentile that most people use.

Let's say you have a collection of animal weights. Let's say you have 55 ants, and 145 elephants. Your method would sort them and take the weight of the 50th animal, which would be an ant. So you'd say that the 50th weight percentile was 3 milligrams. My method would look at the weight to find where 50% of the weight was less and 50% of the weight was more. That of course would lie with the elephants, probably at the 73rd elephant, and so my method would give the 50th percentile weight at 5000 kilograms.

Which way do you want it?

Image Analyst
on 2 May 2014

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/127944-how-to-pick-the-j-th-percentile-of-a-vector#comment_211523

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/127944-how-to-pick-the-j-th-percentile-of-a-vector#comment_211523

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/127944-how-to-pick-the-j-th-percentile-of-a-vector#comment_356971

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/127944-how-to-pick-the-j-th-percentile-of-a-vector#comment_356971

Sign in to comment.