OSX v10.10.5和Gnuplot v5.0
我有一个包含三列数字的数据文件,我读取存储在其中的值来进行一些计算。但这很耗时!
这是我到目前为止所知道的:
#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:
#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)但它并没有改变所需的时间。知道是怎么回事吗?
发布于 2017-03-17 04:44:10
您没有发布完整的代码(缺少a、b、...、f的定义)。但是在您发布的部分代码中,我认为您可以避免频繁地调用awk。您可以将函数f(k)和g(k)替换为简单的变量fk和gk,因为实际上它们在每次k次迭代中都是常量。似乎没有必要在每次I-迭代中重新计算它们。
#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但在缺失的代码中可能有更多有趣的细节,这阻碍了这个解决方案。
https://stackoverflow.com/questions/42814243
复制相似问题