熔化的距离表进入距离矩阵

发布于 2025-02-08 16:08:44 字数 731 浏览 2 评论 0原文

我有这样的桌子:

Var     score
1       1.00
1       1.06
1       1.03
1       0.65
1       0.68
2       1.06
2       1.07
2       0.64
2       1.05
3       0.71
3       0.72
3       1.03
4       0.68
4       1.08
5       0.11

想将其转换为矩阵,例如:

      1     2     3     4     5     6     
 1    0.00  1.00  1.06  1.03  0.65  0.68
 2    1.00  0.00  1.06  1.07  0.64  1.05
 3    1.06  1.06  0.00  0.71  0.72  1.03
 4    1.03  1.07  0.71  0.00  0.68  1.08
 5    0.65  0.64  0.72  0.68  0.00  0.11
 6    0.68  1.05  1.03  1.08  0.11  0.00  

我尝试了尴尬,但它一直在运行:

awk '{if(NF>max) max=NF} END{while(getline<"file"){for(i=NF+1;i<=max;i++)$i="0";print}}'

I have a table like this:

Var     score
1       1.00
1       1.06
1       1.03
1       0.65
1       0.68
2       1.06
2       1.07
2       0.64
2       1.05
3       0.71
3       0.72
3       1.03
4       0.68
4       1.08
5       0.11

Want to convert this into matrix like:

      1     2     3     4     5     6     
 1    0.00  1.00  1.06  1.03  0.65  0.68
 2    1.00  0.00  1.06  1.07  0.64  1.05
 3    1.06  1.06  0.00  0.71  0.72  1.03
 4    1.03  1.07  0.71  0.00  0.68  1.08
 5    0.65  0.64  0.72  0.68  0.00  0.11
 6    0.68  1.05  1.03  1.08  0.11  0.00  

I tried awk but its keep running:

awk '{if(NF>max) max=NF} END{while(getline<"file"){for(i=NF+1;i<=max;i++)$i="0";print}}'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2025-02-15 16:08:44

它继续运行,因为您忘记将其传递给文件名。因此,awk从标准输入中获取其输入,并等待您在键盘上输入某些内容。使用awk'...'文件,而不仅仅是awk'...'。但是,即使解决了此错误,它也不会如您期望的那样起作用。

您无需两次读取文件。您可以在一个通过中构建矩阵,并在end块中填充缺失的单元格(用GNU和BSD AWK测试):

awk 'NR > 1 {
  num[$1] += 1
  mat[$1, $1 + num[$1]] = mat[$1 + num[$1], $1] = $2
  n = num[$1] > n ? num[$1] : n
}
END {
  n += 1
  mat[0, 0] = ""
  for(i = 1; i <= n; i += 1) {
    mat[0, i] = mat[i, 0] = i
    mat[i, i] = "0.00"
  }
  for(i = 0; i <= n; i += 1)
    for(j = 0; j <= n; j += 1)
      printf("%s%s", mat[i, j], j == n ? "\n" : "\t")
}' file
     1       2       3       4       5       6
1    0.00    1.00    1.06    1.03    0.65    0.68
2    1.00    0.00    1.06    1.07    0.64    1.05
3    1.06    1.06    0.00    0.71    0.72    1.03
4    1.03    1.07    0.71    0.00    0.68    1.08
5    0.65    0.64    0.72    0.68    0.00    0.11
6    0.68    1.05    1.03    1.08    0.11    0.00

It keeps running because you forgot to pass it the file name. So awk takes its input from the standard input and waits for you to enter something on the keyboard. Use awk '...' file, not just awk '...'. But even with this error fixed it will not work as you expect.

You don't need to read the file twice. You can build your matrix in one single pass and populate the missing cells in the END block (tested with GNU and BSD awk):

awk 'NR > 1 {
  num[$1] += 1
  mat[$1, $1 + num[$1]] = mat[$1 + num[$1], $1] = $2
  n = num[$1] > n ? num[$1] : n
}
END {
  n += 1
  mat[0, 0] = ""
  for(i = 1; i <= n; i += 1) {
    mat[0, i] = mat[i, 0] = i
    mat[i, i] = "0.00"
  }
  for(i = 0; i <= n; i += 1)
    for(j = 0; j <= n; j += 1)
      printf("%s%s", mat[i, j], j == n ? "\n" : "\t")
}' file
     1       2       3       4       5       6
1    0.00    1.00    1.06    1.03    0.65    0.68
2    1.00    0.00    1.06    1.07    0.64    1.05
3    1.06    1.06    0.00    0.71    0.72    1.03
4    1.03    1.07    0.71    0.00    0.68    1.08
5    0.65    0.64    0.72    0.68    0.00    0.11
6    0.68    1.05    1.03    1.08    0.11    0.00
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文