0

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

3
  • It would be nice if you could show some of the data from data.txt, and provide some explanation of what you're trying to accomplish, for those of us who know python but aren't fluid in matlab. Can you show us what you tried and how that failed? Additionally, there seems to be a bug in your code in the sense that the outer loop runs the inner loop 2000 times in exactly the same way. Why? Commented Oct 22, 2012 at 14:10
  • Hello Roland, the data seems to be a .txt file containing currency values throughout time with columns: KEY Month Day and then a series of three character currency codes ie AUD ATS etc. What I am trying to accomplish is just take this Matlab code and port it into python, no questions asked. I am not fluent in Matlab either, that's why I am trying to understand that specific line. It's actually a silly "benchmarking" code someone wrote, so what you see as a bug... is a actually a feature. Commented Oct 22, 2012 at 14:14
  • What's happening in this line is called indexing. You are selecting specific rows and columns in the ´datamat.data´ matrix: rows from Limits_Area(j) to Limits_Area(j+1) and columns 4 to 37. Limits_Area(j) is indexing in a vector and simply means j-th element of Limits_Area. mathworks.com/company/newsletters/articles/… Numpy has a very similar syntax so if it is supposed to look the same in Matlab, use that. Commented Oct 22, 2012 at 14:22

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I wouldn't make an attempt to port any serious matlab code to python without numpy.
yes I use numpy, so what does this specific line does in Matlab and how would it translate to python? thanks this seems like it's on the right track.
Also worth a look at the Online Matlab to Python Converter. The project appears to be inactive, which is a pity, but there may be something to download that can help. However, I second mgilson's comment. Use NumPy for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.