0

嘿,我正在尝试查找文本文件中记录之间的距离。我正在尝试使用awk来做到这一点。一个示例输入是:

1 2 1 4 yes
2 3 2 2 no
1 1 1 5 yes
4 2 4 0 no
5 1 0 1 no

我想找到每个数值之间的距离。我通过减去这些值然后对答案进行平方来做到这一点。我已经尝试了下面的代码,但所有的距离都只是 0。任何帮助将不胜感激。

BEGIN {recs = 0; fieldnum = 5;}
{
  recs++;
    for(i=1;i<=NF;i++) {data[recs,i] = $i;}
}
END {
  for(r=1;r<=recs;r++) {
    for(f=1;f<fieldnum;f++) {
        ##find distances
        for(t=1;t<=recs;t++) {
        distance[r,t]+=((data[r,f] - data[t,f])*(data[r,f] - data[t,f]));
            }
        }
    }
      for(r=1;r<=recs;r++) {
        for(t=1;t<recs;t++) {
        ##print distances
        printf("distance between %d and %d is %d \n",r,t,distance[r,t]);
        }
        }
    }
4

1 回答 1

3

不知道“每个数值之间的距离”在概念上是什么意思,所以我无法帮助您使用算法,但让我们清理代码以查看它的样子:

$ cat tst.awk
{
   for(i=1;i<=NF;i++) {
      data[NR,i] = $i
   }
}
END {
  for(r=1;r<=NR;r++) {
    for(f=1;f<NF;f++) {
        ##find distances
        for(t=1;t<=NR;t++) {
            delta = data[r,f] - data[t,f]
            distance[r,t]+=(delta * delta)
        }
     }
  }
  for(r=1;r<=NR;r++) {
     for(t=1;t<NR;t++) {
        ##print distances
        printf "distance between %d and %d is %d\n",r,t,distance[r,t]
     }
  }
}
$
$ awk -f tst.awk file
distance between 1 and 1 is 0
distance between 1 and 2 is 7
distance between 1 and 3 is 2
distance between 1 and 4 is 34
distance between 2 and 1 is 7
distance between 2 and 2 is 0
distance between 2 and 3 is 15
distance between 2 and 4 is 13
distance between 3 and 1 is 2
distance between 3 and 2 is 15
distance between 3 and 3 is 0
distance between 3 and 4 is 44
distance between 4 and 1 is 34
distance between 4 and 2 is 13
distance between 4 and 3 is 44
distance between 4 and 4 is 0
distance between 5 and 1 is 27
distance between 5 and 2 is 18
distance between 5 and 3 is 33
distance between 5 and 4 is 19

似乎产生一些非零输出....

于 2012-10-19T02:11:06.317 回答