10

I have a matrix in MATLAB and I need to find the 99% value for each column. In other words, the value such that 99% of the population has a larger value than it. Is there a function in MATLAB for this?

gnovice
  • 125,304
  • 15
  • 256
  • 359
sfactor
  • 12,592
  • 32
  • 102
  • 152

3 Answers3

11

Use QUANTILE function.

Y = quantile(X,P);

where X is a matrix and P is scalar or vector of probabilities. For example, if P=0.01, the Y will be vector of values for each columns, so that 99% of column values are larger.

yuk
  • 19,098
  • 13
  • 68
  • 99
11

The simplest solution is to use the function QUANTILE as yuk suggested.

Y = quantile(X,0.01);

However, you will need the Statistics Toolbox to use the function QUANTILE. A solution that is not dependent on toolboxes can be found by noting that QUANTILE calls the function PRCTILE, which itself calls the built-in function INTERP1Q to do the primary computation. For the general case of a 2-D matrix that contains no NaN values you can compute the quantiles of each column using the following code:

P = 0.01;       %# Your probability
S = sort(X);    %# Sort the columns of your data X
N = size(X,1);  %# The number of rows of X
Y = interp1q([0 (0.5:(N-0.5))./N 1]',S([1 1:N N],:),P);  %'# Get the quantiles

This should give you the same results as calling QUANTILE, without needing any toolboxes.

Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Would you please explain the meaning of 1% probability here? I am trying to implement your script to find the interquantile range for a vector but have not managed yet after hours. Thank you very much – chappi Dec 23 '19 at 17:31
2

If you do not have the Statistics Toolbox, there is always

y=sort(x);
y(floor(length(y)*0.99))

or

y(floor(length(y)*0.01))

depending on what you meant.

AVB
  • 3,994
  • 22
  • 21
  • Unfortunately, this won't generally give the same results as QUANTILE. – gnovice Mar 05 '10 at 16:18
  • @gnovice: oh yes, QUANTILE interpolates and is generally more precise. Good job mimicking it in your answer :-) – AVB Mar 05 '10 at 16:47