百分制计算 [英] Percentile calculation

查看:172
本文介绍了百分制计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模仿 Excel的相当于百分位数函数C#(或者在一些伪code)。我该怎么办呢?该函数应该用两个参数,其中第一个是值的列表,第二是为了什么百分函数应该计算。

I want to mimic the Excel equivalent PERCENTILE function in C# (or in some pseudo code). How can I do that? The function should take two arguments where the first is a list of values and the second is for what percentile the function should calculate for.

坦克!

编辑:我很抱歉,如果我的问题碰到像我以前没有尝试过我自己。我就是不明白是怎么Excel函数工作(是的,我尝试维基百科和钨第一),我想我会更好地理解它,如果有人$ P $在code psented它。 @ codeInChaos了,似乎是我后一个答案。

I'm sorry if my question came across like I had not tried it my self. I just couldn't understand how the excel function worked (yes, I tried wikipedia and wolfram first) and I thought I would understand it better if someone presented it in code. @CodeInChaos gave an answer that seem to be what I'm after.

推荐答案

我觉得维基百科页面有公式,你需要写自己的功能...
我想这样的:

I think Wikipedia page has formulas you need to write your own function...
I tried this:

public double Percentile(double[] sequence, double excelPercentile)
{
    Array.Sort(sequence);
    int N = sequence.Length;
    double n = (N - 1) * excelPercentile + 1;
    // Another method: double n = (N + 1) * excelPercentile;
    if (n == 1d) return sequence[0];
    else if (n == N) return sequence[N - 1];
    else
    {
         int k = (int)n;
         double d = n - k;
         return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);
    }
}

codeInChaos评论后编辑:
Excel使用0和1之间(所以我改变了我的code维基百科的公式来实现这一点),另一个方法的百分位值calulate N(所以我改变了评论的)。

EDITED after CodeInChaos comment:
Excel uses a percentile value between 0 and 1 (so I changed my code to implement this with Wikipedia formulas) and the other method to calulate n (so I changed the commented one).

这篇关于百分制计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆