Added mex interface for speed up computation