使用awk查找线性趋势直至最大值 [英] Find linear trend up to the maximum value using awk

查看:98
本文介绍了使用awk查找线性趋势直至最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据文件,如下所示:

I have a datafile as below:

ifile.txt

ifile.txt

-10     /
-9      /
-8      /
-7      3
-6      4
-5      13
-4      16
-3      17
-2      23
-1      26
0       29
1       32
2       35
3       38
4       41
5       40
6       35
7       30
8       25
9       /
10      /

此处是"/"是缺少的值.我想计算线性趋势,直到y轴的最大值(即直到第二列的值"41").因此,它应该根据以下数据计算趋势:

Here "/" are the missing values. I would like to compute the linear trend up to the maximum value in the y-axis (i.e. up to the value "41" in 2nd column). So it should calculate the trend from the following data:

-7      3
-6      4
-5      13
-4      16
-3      17
-2      23
-1      26
0       29
1       32
2       35
3       38
4       41

不考虑其他(x,y),因为(4,41)之后y值小于41

Other (x, y) won't be consider because the y values are less than 41 after (4, 41)

以下脚本适用于所有值:

The following script is working fine for all values:

awk '!/\//{sx+=$1; sy+=$2; c++;
                    sxx+=$1*$1; sxy+=$1*$2}
           END     {det=c*sxx-sx*sx;
                    print (det?(c*sxy-sx*sy)/det:"DIV0")}' ifile.txt

但是我不能做到最大价值

But I can't able to do it for maximum value

For the given example the result will be 3.486

推荐答案

已根据您的评论进行了更新.我认为您的趋势计算很好,并使用了它们:

Updated based on your comments. I assumed your trend calculations were good and used them:

$ awk '
$2!="/" {
    b1[++j]=$1                            # buffer them up until or if used
    b2[j]=$2
    if(max=="" || $2>max) {               # once a bigger than current max found
        max=$2                            # new champion
        for(i=1;i<=j;i++) {               # use all so far buffered values
            # print  b1[i], b2[i]         # debug to see values used
            sx+=b1[i]                     # Your code from here on
            sy+=b2[i]
            c++
            sxx+=b1[i]*b1[i]
            sxy+=b1[i]*b2[i]
        }
        j=0                               # buffer reset
        delete b1
        delete b2
    }
}
END {
    det=c*sxx-sx*sx
    print (det?(c*sxy-sx*sy)/det:"DIV0")
}' file

有关数据:

0       /
1       1
2       2
3       4
4       3
5       5
6       10
7       7
8       8

带调试print的未注释程序将输出:

with debug print uncommented program would output:

1 1
2 2
3 4
4 3
5 5
6 10
1.51429

这篇关于使用awk查找线性趋势直至最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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