at long last, the Matlab implementation