均使用windows active perl进行测试
安装统一使用:
c:\perl\bin>ppm-shell
ppm>install Statistics::Basic
ppm>install Statistics::Descriptive
1、求整数的中位数
(1)Statistics::Basic
#!/usr/bin/perl
use strict;
my @array;
$array[0]=1;
$array[1]=2;
$array[2]=3;
$array[3]=4;
use Statistics::Basic qw(:all);
my $median2= median(@array);
print "$median2 \n";
屏幕输出结果:2.5
(2)Statistics::Descriptive
#!/usr/bin/perl
use strict;
my @array;
$array[0]=1;
$array[1]=2;
$array[2]=3;
$array[3]=4;
use Statistics::Descriptive;
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@array);
my $ median = $stat->median();
print "$median \n";
屏幕输出结果:2.5
评论:从上面两个结果看,对于一串整数,求中位数返回的结果是一致的
2、求小数的中位数
(1)Statistics::Basic
#!/usr/bin/perl
use strict;
my @array;
$array[0]=0.0001;
$array[1]=0.00021;
$array[2]=0.0003;
$array[3]=0.0004;
use Statistics::Basic qw(:all);
my $median2= median(@array);
print "$median2 \n";
屏幕输出结果:0
2、Statistics::Descriptive
#!/usr/bin/perl
use strict;
my @array;
$array[0]=0.0001;
$array[1]=0.00021;
$array[2]=0.0003;
$array[3]=0.0004;
use Statistics::Descriptive;
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@array);
my $ median = $stat->median();
print "$median";