熔化的距离表进入距离矩阵
我有这样的桌子:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它继续运行,因为您忘记将其传递给文件名。因此,
awk
从标准输入中获取其输入,并等待您在键盘上输入某些内容。使用awk'...'文件
,而不仅仅是awk'...'
。但是,即使解决了此错误,它也不会如您期望的那样起作用。您无需两次读取文件。您可以在一个通过中构建矩阵,并在
end
块中填充缺失的单元格(用GNU和BSD AWK测试):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. Useawk '...' file
, not justawk '...'
. 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):