首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gnuplot使用从文件读取数据加速进程

gnuplot使用从文件读取数据加速进程
EN

Stack Overflow用户
提问于 2017-03-15 23:33:57
回答 1查看 95关注 0票数 1

OSX v10.10.5和Gnuplot v5.0

我有一个包含三列数字的数据文件,我读取存储在其中的值来进行一些计算。但这很耗时!

这是我到目前为止所知道的:

代码语言:javascript
复制
#user defined function to read data in a file
#see stackoverflow: "Reading dataset value into a gnuplot variable (start of X series)"
at(file, row, col) = system( sprintf("awk -v row=%d -v col=%d 'NR == row {print $col}' %s", row, col, file) )
file="myFile"

do for [k=1:10] { #we read line by line and we want the ratio between column 2/1 and 3/1
f(k) = at(file,k,2)/at(file,k,1)
g(k) = at(file,k,3)/at(file,k,1)

# example of calculation: least square to find the best "i"
do for [i=1:10] {
    f1(i) = (a*i**2 + b*i + c) #function for the least square. a,b,c: floats
    g1(i) = (d*i**2 + e*i + f) #d,e,f: floats
    h(i) = sqrt( (f1(i)-f(k))**2 + (g1(i)-g(k))**2 )
    if (h(i)<hMin) {
        hMin=h(i)
                   }
    else {}
            } #end loop i
print i," ",hMin
} #end loop k

它是有效的,但正如我所说的,它需要时间(每k大约2分钟)。当我不做任何计算,只问print f(k),g(k)时,它是<< 1秒。我怀疑除法可能会导致太多的数字和低效的计算。我首先使用了round2函数来保存n=4:

代码语言:javascript
复制
#see stackoverflow: How to use floor function in gnuplot
round(x) = x - floor(x) < 0.5 ? floor(x) : ceil(x)
round2(x, n) = round(x*10**n)*10.0**(-n)
f(k) = round2((at(file,k,2)/at(file,k,1)),4)
g(k) = round2((at(file,k,3)/at(file,k,1)),4)

但它并没有改变所需的时间。知道是怎么回事吗?

EN

回答 1

Stack Overflow用户

发布于 2017-03-17 04:44:10

您没有发布完整的代码(缺少a、b、...、f的定义)。但是在您发布的部分代码中,我认为您可以避免频繁地调用awk。您可以将函数f(k)g(k)替换为简单的变量fkgk,因为实际上它们在每次k次迭代中都是常量。似乎没有必要在每次I-迭代中重新计算它们。

代码语言:javascript
复制
#user defined function to read data in a file
#see stackoverflow: "Reading dataset value into a gnuplot variable (start of X series)"
at(file, row, col) = system( sprintf("awk -v row=%d -v col=%d 'NR == row {print $col}' %s", row, col, file) )
file="myFile"

do for [k=1:10] { #we read line by line and we want the ratio between column 2/1 and 3/1
    at1 = at(file,k,1)
    fk = at(file,k,2)/at1
    gk = at(file,k,3)/at1

    # example of calculation: least square to find the best "i"
    do for [i=1:10] {
        f1i = (a*i**2 + b*i + c) #function for the least square. a,b,c: floats
        g1i = (d*i**2 + e*i + f) #d,e,f: floats
        hi = sqrt( (f1i-fk)**2 + (g1i-gk)**2 )
        if (hi<hMin) {
            hMin=hi   
        } else {
        }
    } #end loop i 
    print i," ",hMin
} #end loop k

但在缺失的代码中可能有更多有趣的细节,这阻碍了这个解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42814243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档