Find percentile corresponding to an input value

33 ビュー (過去 30 日間)
Jared
Jared 2017 年 7 月 8 日
コメント済み: Miguel Lovino 2020 年 6 月 22 日
I'm trying to find a MATLAB function that is similar to the PERCENTRANK formula in Excel. With this formula you enter an array and a scalar and it tells you what percentile that scalar corresponds to.

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 8 日
PERCENTRANK = @(YourArray, TheProbes) reshape( mean( bsxfun(@le, YourArray(:), TheProbes(:).') ) * 100, size(TheProbes) )
This accepts a vector or array of data, and an array of probe positions (could be a scalar), and returns an array of percentiles the same size as the probe positions, considered over the entire content of the data array (not per row or per column)
  3 件のコメント
Wenersamy de Alcantara
Wenersamy de Alcantara 2020 年 5 月 11 日
編集済み: Wenersamy de Alcantara 2020 年 5 月 11 日
Hi, Alex, the algorithm of the suggested PERCENTRANK function basically averages the result of a logical comparison.
For example, in the vector (6 numbers drawn from a discrete uniform distribution):
[23 45 98 2 81 17]
If you compare it with, say, 30: [23 45 98 2 81 17] <= 30, you'll have:
[1 0 0 1 0 1]
If you avarage the results of the comparison, you'll have:
3/6 = 0.5 or 50% percentile.
Of course, the approximation gets better with a bigger sample.
In other words, you can implement excel function PERCENTRANK.INC(matrix,k) by just using: mean(matrix<=k).
Note that they are not exactly the same algorithm, but they get closer as the size of "matrix" increases.
Miguel Lovino
Miguel Lovino 2020 年 6 月 22 日
Hi Walter,
this code works perfectly for a vector (lat - lon -time varying). I want to adapt it to an array array of 121 * 121 * 2995 (it's lat-lon -time). That is, for a given lat-lon, it works. I try to rewrite as:
A = ncread('ERA5_pentads_sm_l123_1979-2019.nc','sm');
B = ncread('ERA5_pentads_sm_l123_1979-2019.nc','sm');
[n,m,t]=size(A);
As = zeros(n,m,t);
for ii=1:n
for jj=1:m
PER = @(A,B) reshape( mean( bsxfun(@le, A(ii,jj,:), ipermute(A(ii,jj,:),[3 2 1])))...
* 100, t, []);
As (ii,jj,:)= PER (A);
end
end
It works, but it seems that lat-lon points are not correct.
I really appreciate some help,
Best
Miguel

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

その他の回答 (1 件)

Stefano Crema
Stefano Crema 2019 年 10 月 15 日
Great, compact and handy solution, thanks Walter!

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by