MATLAB implementation