John D'Errics's excellent ipdm http://www.mathworks.co.uk/matlabcentral/fileexchange/18937-ipdm-inter-point-distance-matrix is more advanced, and supports lots of distance metrics, use the 'nearestto' option to get similar results to your funtion.