使用awk的连续元素移动均线 [英] Moving average with successive elements using awk

查看:75
本文介绍了使用awk的连续元素移动均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,其中每个行元素都将给出接下来N行(包括其自身)的平均值.我知道如何对第N行之类的前几行进行操作,将得出前N行的平均值.这是该脚本

I am trying to write a script in which each row element will give the average of next N rows (including itself). I know how to do it with preceding rows like the Nth row will give the average of the preceding N rows. Here is the script for that

awk '
BEGIN{
       N = 5;
     }

     { 
       x = $2;
       i = NR % N;
       aveg += (x - X[i]) / N;
       X[i] = x;
       print $1, $2, aveg; 
     }' < file > aveg.txt

文件看起来像这样

     1       1
     2       2
     3       3
     4       4
     5       5
     6       6
     7       7
     8       8
     9       9
    10      10
    11      11
    12      12
    13      13
    14      14
    15      15
    16      16
    17      17
    18      18
    19      19
    20      20
    21      21
    22      22
    23      23
    24      24
    25      25
    26      26
    27      27
    28      28
    29      29
    30      30
    31      31
    32      32
    33      33
    34      34
    35      35
    36      36
    37      37
    38      38
    39      39
    40      40

我希望第一行具有接下来5个元素的平均值,即

I want that the first row has average of the next 5 elements i.e.

 (1+2+3+4+5)/5=3
 second row (2+3+4+5+6)/5=4
 third row  (3+4+5+6+7)/5=5

,依此类推.行应该看起来像

and so on. The rows should look like

    1        1       3  
    2        2       4
    3        3       5
    4        4       6   ...

是否可以像上面显示的脚本那样简单地完成?我正在考虑将行值分配为下面的第n行的值,然后继续执行上面的脚本.但是,不幸的是,我无法将行值分配给文件中的某些值.有人可以帮我编写此脚本并找到移动平均线吗?我也对Shell中的其他命令持开放态度.

Can it be done as simply as the script shown above? I was thinking of assigning the row value as the value of nth row below and then proceeding with the above script. But, unfortunately I am unable to assign the row value to some value down the file. Can someone help me to write this script and find the moving average. I am open to other commands in shell as well.

推荐答案

$ cat test.awk
BEGIN {
    N=5            # the window size
}
{
    n[NR]=$1       # store the value in an array
} 
NR>=N {            # for records where NR >= N
    x=0            # reset the sum variable
    delete n[NR-N] # delete the one out the window of N
    for(i in n)    # all array elements
        x+=n[i]    # ... must be summed
    print n[NR-(N-1)],x/N  # print the row from the beginning of window
}                          # and the related window average

尝试一下:

$ for i in {1..36}; do echo $i $i >> test.in ; done
$ awk -f test.awk test.in
1 3
2 4
3 5
...
30 32
31 33
32 34

可以用总和,加上电流和减去n[NR-N]来完成,就像这样:

It could be done in running sum, add current and subtract n[NR-N], like this:

BEGIN {
    N=5
} 
{
    n[NR]=$1
    x+=$1-n[NR-N]
} 
NR>=N {
    delete n[NR-N]
    print n[NR-(N-1)],x/N
}

这篇关于使用awk的连续元素移动均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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