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分钟)。当我不进行任何计算并仅询问打印f(k),g(k)时,它是&lt;&lt; 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)
但它没有改变所需的时间。 关于发生了什么的任何想法?
答案 0 :(得分:0)
您没有发布完整代码(缺少a,b,...,f的定义)。但是在您发布的代码部分内,我认为您可以避免经常调用f(k)
。您可以使用简单变量g(k)
和fk
替换函数gk
和#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
,因为事实上它们在每次k迭代中都是常量。似乎没有必要在每次迭代中重新计算它们。
WEB-INF/{\*.jar}!/META-INF/resources/
但是在丢失的代码中可能会有更多有趣的细节抑制这个解决方案。