added some mex stuff and sped up matlab version of sampling