original MATLAB code