2015-05-05 19 views
0

我有一個需要使用第一列的文件。其餘的列需要與第一列進行整合。比方說我的文件看起來像這樣:使用數據文件中的多列

100 1.0 1.1 1.2 1.3 0.9 
110 1.8 1.9 2.0 2.1 2.2 
120 1.8 1.9 2.0 2.1 2.2 
130 2.0 2.1 2.3 2.4 2.5 

我可以寫一段代碼,是以第二列,並與第一那麼第三集成和整合相對於第一等?對於我的代碼有:

import scipy as sp 
first_col=dat[:,0] #first column from data file 
cols=dat[:,1:] #other columns from data file 

col2 = cols[:,0] # gets the first column from variable cols 
I = sp.integrate.cumtrapz(col2, first_col, initial = 0) #integration step 

這僅適用於從變量山坳中的第一行,但是,我不想寫這爲所有其它列,它看起來討論(的思想它讓我發抖)。我看到過類似的問題,但一直未能將答案與我的答案聯繫起來,而那些差不多相同的答案卻含糊不清。有任何想法嗎?

+0

你可以使用'genfromtxt',然後自己執行所有的集成作爲替代 – Matthew

+0

什麼參數可以讓我循環訪問列? – skitt1z1

+1

如果ive明白你想要的是什麼,你想要在幾個列上應用相同的集成,所以'data = sp.genfromtxt(「file.txt」);爲col in數據[0]:;集成(data [:,0],data [:,col]),其中'integrate'是一個def函數。 (雖然答案似乎是做你想要的,而不必自己寫!) – Matthew

回答

1

功能cumtrapz接受axis參數。例如,假設你把你的第一列xy其餘列,他們有這些值:

In [61]: x 
Out[61]: array([100, 110, 120, 130]) 

In [62]: y 
Out[62]: 
array([[ 1.1, 2.1, 2. , 1.1, 1.1], 
     [ 2. , 2.1, 1. , 1.2, 2.1], 
     [ 1.2, 1. , 1.1, 1. , 1.2], 
     [ 2. , 1.1, 1.2, 2. , 1.2]]) 

你可以用尊重的y每列集成到x如下:

In [63]: cumtrapz(y, x=x, axis=0, initial=0) 
Out[63]: 
array([[ 0. , 0. , 0. , 0. , 0. ], 
     [ 15.5, 21. , 15. , 11.5, 16. ], 
     [ 31.5, 36.5, 25.5, 22.5, 32.5], 
     [ 47.5, 47. , 37. , 37.5, 44.5]]) 
+0

這非常有效!我應該在使用cumtrapz後自己看過這個... – skitt1z1

相關問題