I have the following code snippet in Matlab and want to port it to Python.
for i=1:runs;
tic;
datmat=importdata('data.txt',' ');
Limits_Area=[1,261,522,784,1045,1305,1565,1827,2088,2349,2610,2871,3131,3158];
for k=1:2000;
for j=1:13;
Year_Data=datmat.data(Limits_Area(j):Limits_Area(j+1)-1,4:37);
Min_Year=min(Year_Data);
Max_Year=max(Year_Data);
Mean_Year=mean(Year_Data);
GainLoss_Year=100-(Year_Data(1,:)+0.00000001)./(Year_Data(end,:)+0.00000001)*100;
end;
end;
I am having a really hard time with the
Year_Data=datmat.data(Limits_Area(j):Limits_Area(j+1)-1,4:37);
part.... Any directions?
Thank you
Have you tried numpy ? it is a library for scientific computing which looks like Matlab. For example :
In Matlab :
a(1:5,:)
In Numpy :
a[0:5] or a[:5] or a[0:5,:]
Check out : Numpy for Matlab Users
if you do not want to use Numpy, try the comprehension lists :
Year_Data = [ [datmat.data(i,j) for j in range (4,38) ] for i in range(j,j+2) ]
EDIT:
for i in range(runs) :
datamat = numpy.genfromtxt('data.txt',delimiter=' ', newline ='\n' )
// Adapt the previous line to the format of your txt file
// at this point you should have a numpy.array object with the right shape
Limits_Area= numpy.array( [1,261,522,784,1045,1305,1565,1827,2088,2349,2610,2871,3131,3158] )
for k in range(2000):
for j in range(13):
Year_Data = datmat[ Limits_Area(j):Limits_Area(j+1)-1 , 4:37 ]
etc etc ...
NB : Matlab arrays indexes goes from 1 to n whereas numpy arrays indexes goes from 0 to n-1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.