将3列文件转换为矩阵格式

编程入门 行业动态 更新时间:2024-10-26 12:29:02
本文介绍了将3列文件转换为矩阵格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个类似于以下示例的文件格式,该文件格式显示了5个人(包括他们自己)之间的关系.

I have a file format like the example below showing the relationships between 5 individuals including themselves.

1 1 1.0 2 1 0.5 3 1 0.1 4 1 0.3 5 1 0.1 2 2 1.0 3 2 0.5 4 2 0.2 5 2 0.3 3 3 1.0 4 3 0.5 5 3 0.3 4 4 1.0 5 4 0.1 5 5 1.0

我想使用AWK将其转换为完整的矩阵格式.我将需要像示例中那样对行和列进行数字排序.

I would like to use AWK to convert it into a full matrix format. I would be necessary to have the rows and columns sorted numerically as in the example.

1 2 3 4 5 1 1.0 0.5 0.1 0.3 0.1 2 0.5 1.0 0.5 0.2 0.3 3 0.1 0.5 1.0 0.5 0.3 4 0.3 0.2 0.5 1.0 0.1 5 0.1 0.3 0.3 0.1 1.0

我遇到了上一个线程(如下),但是输入文件的格式略有不同,我正在努力调整它. www.unix. com/shell-programming-and-scripting/203483-how-rearrange-matrix-awk.html

I came across a previous thread (below) but the format of the input file is slightly different and i am struggling to adjust it. www.unix/shell-programming-and-scripting/203483-how-rearrange-matrix-awk.html

如何执行此转换?

推荐答案

在这里, gawk 解决方案:

Here we go, gawk solution:

matrixize.awk 脚本:

matrixize.awk script:

#!/bin/awk -f BEGIN { OFS="\t" } # output field separator { b[$1]; # accumulating unique indices if ($1 != $2) { a[$2][$1] = $3 # set `diagonal` relation between different indices } a[$1][$2] = $3 # multidimensional array (reflects relation `one-to-many`) } END { asorti(b); h = ""; # sort unique indices for (i in b) { h = h OFS i # form header columns } print h; # print header column values for (i in b) { row = i; # index column # iterating through the row values (for each intersection point) for (j in a[i]) { row = row OFS a[i][j] } print row } }

用法 :

Usage:

awk -f matrixize.awk yourfile

输出:

1 2 3 4 5 1 1.0 0.5 0.1 0.3 0.1 2 0.5 1.0 0.5 0.2 0.3 3 0.1 0.5 1.0 0.5 0.3 4 0.3 0.2 0.5 1.0 0.1 5 0.1 0.3 0.3 0.1 1.0

更多推荐

将3列文件转换为矩阵格式

本文发布于:2023-10-27 03:44:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   转换为   格式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!