【问题标题】:Gnuplot: Reshape Array from Data File to MatrixGnuplot:将数组从数据文件重塑为矩阵
【发布时间】:2017-07-20 20:59:56
【问题描述】:

我有一个程序将大小为m x n 的矩阵保存为一个长度为L(其中L = m x n)的数组在一个文件中。

m = n = 2 的示例:该文件包含以下数字(如果文件中只有一个矩阵):

1
2
3
4

代表2 x 2矩阵:

1 2
3 4

该文件包含许多矩阵。我希望能够使用::start_position::end_position 命令绘制此文件的特定矩阵,并将长度为L 的数组转换为m x n 矩阵,以便我可以使用命令matrix nonuniform

我该怎么做?

【问题讨论】:

    标签: matrix gnuplot


    【解决方案1】:

    我认为将处理委托给某些外部工具很可能会更好。比如这个gawk脚本:

    BEGIN{
        #mat_id = 2
        #m = 2
        #n = 3
    
        mat_size = m * n
        row_lb = ((mat_id-1) * mat_size) + 1
        row_ub = row_lb + mat_size - 1
    
        curr_row = 0
    }
    
    NR >= row_lb && NR <= row_ub{
        col_id = (NR - row_lb) % n
        c = (col_id == (n-1))?"\n":" "
        printf "%d%s", $1, c
    }
    

    接受三个变量:mat_id是文件中矩阵的从1开始的索引,m表示行数,n表示列数。因此,例如使用数据文件test.dat 为:

    1
    2
    3
    4
    5
    6
    10
    20
    30
    40
    50
    60
    

    一个电话

    gawk -v mat_id=2 -v m=2 -v n=3 -f filter.awk test.dat
    

    确实有效果

    10 20 30
    40 50 60
    

    在 Gnuplot 中,您可以将其包装到命令中(假设 gawk 脚本位于调用 Gnuplot 的同一目录中):

    getMatrix(fName, matId, m, n) = \
        sprintf("<gawk -v mat_id=%d -v m=%d -v n=%d -f filter.awk %s", matId, m, n, fName)
    
    plot getMatrix('test.dat', 2, 2, 3) ... [ rest of the plot command] ...
    

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 2020-03-19
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多