Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Created November 29, 2014 21:22
Show Gist options
  • Save timsgardner/6b7e3fbf5e2a413f88e1 to your computer and use it in GitHub Desktop.
Save timsgardner/6b7e3fbf5e2a413f88e1 to your computer and use it in GitHub Desktop.
dtw mathematica
dtwMatrix2[s_List, t_List] :=
With[{n = Length@s, m = Length@t},
Fold[
{acc, pos} \[Function] pos /. {i_, j_} :>
ReplacePart[acc,
pos -> (s[[i - 1]] - t[[j - 1]])^2 +
Min[Part[acc, ##] & @@@ {{i - 1, j}, {i, j - 1}, {i - 1,
j - 1}}]],
SparseArray[{{1, 1} ->
0, {1, _} -> \[Infinity], {_, 1} -> \[Infinity]}, {n, m} + 1],
Tuples[{Range[n], Range[m]} + 1]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment