Check
, sometimes abetted by Quiet
, is the built-in function provided to handle your kind of problem. Like so:
SeedRandom[42]; list = RandomReal[{0, 1}, {5, 2}];
dist = DistanceMatrix[list, DistanceFunction -> (#1 - #2 &)]
{{{0., 0.}, {0.0788359, 0.0627175}, {0.130058, 0.101854}, {0.129057,0.184616}, {0.100736, 0.582302}},
{{0.0788359, 0.0627175}, {0., 0.}, {0.208894, 0.164571}, {0.0502213, 0.247333}, {0.0218996, 0.519584}},
{{0.130058, 0.101854}, {0.208894, 0.164571}, {0., 0.}, {0.259115, 0.0827616}, {0.230794, 0.684155}},
{{0.129057, 0.184616}, {0.0502213, 0.247333}, {0.259115, 0.0827616}, {0., 0.}, {0.0283216, 0.766917}},
{{0.100736, 0.582302}, {0.0218996, 0.519584}, {0.230794, 0.684155}, {0.0283216, 0.766917}, {0., 0.}}}
enr[xx_] := Quiet @ Check[4 (1/xx^12 - 1/xx^6), {0, 0}, Infinity::indet]
Apply[enr @ {##} &, dist, {2}]
{{{0, 0}, {6.94016*10^13, 1.07993*10^15}, {1.7077*10^11, 3.20869*10^12}, {1.87357*10^11, 2.55167*10^9}, {3.66324*10^12, 2529.39}},
{{6.94016*10^13, 1.07993*10^15}, {0, 0}, {5.79312*10^8, 1.01344*10^10},{1.55384*10^16, 7.63091*10^7}, {3.28711*10^20, 10128.8}},
{{1.7077*10^11, 3.20869*10^12}, {5.79312*10^8, 1.01344*10^10}, {0, 0}, {4.36528*10^7, 3.87358*10^13}, {1.75109*10^8, 341.361}},
{{1.87357*10^11, 2.55167*10^9}, {1.55384*10^16, 7.63091*10^7}, {4.36528*10^7, 3.87358*10^13}, {0, 0}, {1.50189*10^19, 76.9634}},
{{3.66324*10^12, 2529.39}, {3.28711*10^20, 10128.8}, {1.75109*10^8, 341.361}, {1.50189*10^19, 76.9634}, {0, 0}}}
distanc = ... // MatrixForm
,distanc
is theMatrixForm
output and not theDistanceMatrix
itself (that is, the postfix//
operator goes first, then the Set=
operator). You should remove it from your sample code because it's a mistake many people make and it might confuse people. $\endgroup$