refactored matlab code in py script