Method for creating the distance matrix