Updated matlab code