Matlab version of GSDF method