I need to compute a fast DistanceMatrix (euclidean distance) of a large vector. Let say
x = Table[Sin[t],{t,1,10000}]
But pre defined function
Distancematrix[x]
takes on my machine way too long, I was trying to make some faster code but unsuccesfully f.e.
DISMAT[dat_] := (
Clear[N1, DD];
N1 = Length[dat];
DD = ConstantArray[0, {N1, N1}];
For[k1 = 1, k1 < N1 + 1, k1++,
For[k2 = K = k1 + 1, k2 < N1 + 1, k2++,
DD[[k1, k2]] = ( dat[[k1]] - dat[[k2]])^2
]
];
DD = (DD + Transpose[DD])
)
Which is actually rewrited code from Matlab which runs much much faster than in mathematica and can compute DistanceMatrix of x in cca 3 sec. this code in Mathematica takes like 10 min.
Or
distmatrix2 =
Compile[{{point, _Real, 1}, {tr, _Real, 2}}, Total@Abs[point - tr],
CompilationTarget -> "C", RuntimeOptions -> "Speed",
RuntimeAttributes -> {Listable}, Parallelization -> True]
which also takes like 10 min. Thanks for any suggestions.
x = Table[Sin[t],{t,1.,10000}]
, with the approximate real1.
in place of the exact integer1
. $\endgroup$x = Array[Sin, 10000, 1.];
. $\endgroup$x
generated above, on my machine,disMat = DistanceMatrix[x];
only takes half a second. If you must write your own one, avoid explicit loops, not to mention nested ones. A function which might help isOuter
. $\endgroup$x
. Often, one only needs distances for few pairs of points, e.g., those which have distance below a given threshold. In this casesNearest
(which employs a BSP tree) reduces the complexity significantly; see, e.g., here and here. $\endgroup$