I'm not quite sure what you mean by distances in "diagonal matrix" form. Pairwise distances are certainly displayed as matrices, but these are not diagonal (they're symmetric, with zeros along the diagonal). In any case, here's a way to obtain that. I'll define a function cityData
that returns the great-circle distance between two cities:
ClearAll@cityDistance
SetAttributes[cityDistance, Orderless]
cityDistance[x_, y_] := cityDistance[x, y] =
GeoDistance @@ (CityData[#, "Coordinates"] & /@ {x, y})
The Orderless
attribute is used, because the distance from A to B is the same as from B to A.
Then, with your list of cities:
cities = CityData[{Large, "Germany"}]
(* {{"Berlin", "Berlin", "Germany"}, {"Hamburg", "Hamburg", "Germany"},
{"Munich", "Bavaria", "Germany"}, {"Cologne", "NorthRhineWestphalia", "Germany"}, ...} *)
you can form a matrix of pair-wise distances as:
distanceMatrix = Outer[cityDistance, cities, cities, 1];
which gives the distance in meters.
GeoDistance
, where tricks such as vectorization,Tr
, compile, etc. won't work. $\endgroup$ – rm -rf♦ Sep 30 '13 at 20:58