将3列文件转换为矩阵格式 [英] Convert a 3 column file to matrix format

查看:130
本文介绍了将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

我遇到了上一个线程(如下),但是输入文件的格式略有不同,我正在努力调整它. http://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. http://www.unix.com/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列文件转换为矩阵格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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