数组的中位数 [英] Median of an Array

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

问题描述

嘿,我正在与Delphi 10 Seattle一起开发.而且我实际上需要帮助来获取数组的中位数,例如:

Hey guys i am developing with Delphi 10 Seattle. And i actually need help at getting a Median of an Array eg :

 allitems: array[1..500] of Double;

我在一个过程中给出了数组中每个arrayitem的值.因此,我得到了一个包含500个双精度值的数组,并希望从这500个值中获取中位数.首先,我想我必须将数组从低到高排序,然后得到中位数.那么我如何才能首先对数组排序并在排序后得到中位数?

I gave the value of each arrayitem in the array in a procedure. So i got an array of 500 doubles with values in it and want to get the Median out of this 500 values. First of all i guess i have to sort the array from low to high values and after it getting the median. So How can i sort the array first and get the median after sorting ?

推荐答案

偶数个元素的中位数定义为中心元素的平均值:

The Median of an even number of elements is defined as the mean of the center elements:

var
  allitems : TArray<double>;

  TArray.Sort<double>(allitems);
  median := (allitems[249] + allitems[250]) / 2; // TArray<double> starts with index 0

这篇关于数组的中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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