是否需要使用bash和awk从数组计算标准差? [英] Need to calculate standard deviation from an array using bash and awk?

查看:184
本文介绍了是否需要使用bash和awk从数组计算标准差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我是awk的新手,我正努力使用awk命令来查找标准偏差.

Guys I'm new to awk and I'm struggling with awk command to find the standard deviation.

我使用以下方法得出平均值:

I have got the mean using the following:

echo ${GfieldList[@]} | awk 'NF {sum=0;for (i=1;i<=NF;i++)sum+=$i; print "Mean= " sum / NF; }'

标准偏差公式为:

sqrt((1/N)*(sum of (value - mean)^2))

我已经使用上面的公式找到了平均值

I have found the mean using the above formula

你们可以用awk命令帮助我吗?

Can you guys help me with the awk command for this one?

推荐答案

一旦您知道平均值:

awk '{
    for (i = 1;i <= NF; i++) {
        sum += $i
    };
    print sum / NF
}' # for 2, 4, 4, 4, 5, 5, 7, 9 gives 5

然后可以找到标准偏差:

then the standard deviation can be found thus:

awk -vM=5 '{
    for (i = 1; i <= NF; i++) {
        sum += ($i-M) * ($i-M)
    };
    print sqrt (sum / NF)
}' # for 2, 4, 4, 4, 5, 5, 7, 9 gives 2

以压缩"形式:

awk '{for(i=1;i<=NF;i++){sum+=$i};print sum/NF}'
awk -vM=5 '{for(i=1;i<=NF;i++){sum+=($i-M)*($i-M)};print sqrt(sum/NF)}'

(将M的值更改为从第一个命令提取的实际均值).

(changing the value for M to the actual mean extracted from the first command).

这篇关于是否需要使用bash和awk从数组计算标准差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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