Search code examples
numpymatlabnumpy-ndarraynumpy-slicing

Script to convert Matlab slices and index to numpy array notation


I am porting some Matlab code to Python. In this code there are many instances of a large matrix being sliced in many different ways, eg M(2:45,13:18), M(:12,:), V(4), V(:27), etc. It is technically possible to convert this to numpy notation manually, by replacing the parentheses with square brackets and substracting 1 to all indices (except after :) but it is incredibly tedious and there is a very high probability that I will make a typo at some point.

I tried automating this but my grep/awk/parsing skills are not good enough. Is there a script out there that will do the job?


Solution

  • Assuming you have your matlab code in a string, you can format it in Python. You could do this with regex match replacement:

    import re
    
    def format_indexers(match):
        # Format any "(x:y:z)" into python format
        ret = match[0].replace("(", "[").replace(")", "]") # replace brackets
        ret = re.sub(r"(\[|,)(\d+)", lambda x: x[1] +  str(int(x[2]) - 1) , ret) # replaces numbers by decremented ones
        return ret    
    
    s = "M(2:45,13:18), M(:12,:), V(4), V(:27)"
    
    # Searches expressions between parenthesis and apply format_indexers to found matches
    re.sub(r"\(.*\)", format_indexers, s)
    

    outputs:

    'M[1:45,12:18], M[:12,:], V[3], V[:27]'