skitt1z1
skitt1z1

Reputation: 85

Working with multiple columns from a data file

I have a file in which I need to use the first column. The remaining columns need to be integrated with respect to the first. Lets say my file looks like this:

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

Could I write a piece of code that takes the second column and integrates with the first then the third and integrates with respect to the first and so on? For my code I have:

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

This works only for the first row from the variable col, however, I don't want to write this out for all the other columns, it would look discussing (the thought of it makes me shiver). I have seen similar questions but haven't been able to relate the answers to mine and the ones that are more or less the same have vague answers. Any ideas?

Upvotes: 0

Views: 402

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114841

The function cumtrapz accepts an axis argument. For example, suppose you put your first column in x and the remaining columns in y, and they have these values:

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]])

You can integrate each column of y with respect to x as follows:

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]])

Upvotes: 1

Related Questions