File Exchange

image thumbnail

Dynamic Time Warping

version (1.47 KB) by Timothy Felty
Dynamic time warping program. Calculates the similarity between 2 vectors.


Updated 12 Dec 2005

View License

If you pass in 2 vectors it returns the unnormalized distance between the vectors, the accumulated distance between them, the length of the warping path (the normalizing factor), and the warping path points.

To compare 2 vectors A and B call:
Dist is the unnormalized distance
D is the accumulated distance
k is the length of the warping path
(the normalizing factor)
w is a matrix containing the points along the warping path

Cite As

Timothy Felty (2020). Dynamic Time Warping (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (51)






Qiang Yang

Hi Ethan, Sorry for something wrong of download link. I have changed that link to my Google Drive, so you can download it now:)

Hi Qiang Yang, I don't seem to be able to download the code from your website. Broken link? I am curious what changes you have made. Thanks.

Qiang Yang

hello everyone, maybe Timothy Felty forget to update this function.
I have modified this code to fix some BUGs and add some new functions according to those comments below.
you can download revised code at
contact me if you have any question.(email address in code)

q e

Jeff Court

Code worked great for me (R2014b). My data was of different lengths and didn't have an issue like another user.

My application was using pressure data to predict the path of the pressure sensor. I have additional visualization and computations folks can use if interested. Code expaination and download is here -


Hi Grace Pan
Could you explain what your issue was?

Grace Pan

I have fixed my issue!

Grace Pan

Hi,Timothy Felty
Could i ask you a question? The code works perfectly when two input data have the same length. But if two input data have different length, such as [10 12 14 18 14 12 10 10 12 14 18 14 12 10] and [NaN 12 14 18 14 12 10 10 12 14 18 NaN NaN NaN], the output is NaN. Could you think how to deal with it?


Good frame work, however ignoring the continuous condition.


Good frame work, however ignoring the continuous condition.

in this code, how do i know if the two vectors are a good match or not..


I ran this function for some series of data but all the time, the answer is the same... I mean for different data I get a fix D and Dist and etc.
am I wrong? or is there a problem with the function?


anybody pleas answer and help me. I'm in a hurry



sai sahiti

Hi everyone!!
I'm using this software, and I think that it's really useful.
But I've a question: how to use the normalization factor?
I've read that there are more users that have answered this, but I don't know how to read the previous answers.
Thanks a lot.


please send complete code for melodic contour matching....




At least answer the previous questions!


Maybe add code to ensure proper vectors, e.g. t=t(:)'; and r=r(:)'; in this case ;)


im trying to compare two vectors of audio samples but the matrix dimensions dont match n d code gives error. plz assist


Can I know how can I implement lower bounding (LB_keogh) and global constraint to this algorithm ?

Also, how to use obtain normalized distance using the normalizing factor ?

Mano Samuel

Hi Timothy,
could you let me know which article or book you used as a reference to code this ? . Thank you.


should not be, d(n,m)=sqrt(sum((t(:,n)-r(:,m)).^2)); instead of d(n,m)=(t(n)-r(m))^2;
t, r are sequences of rows-dim vectors

Tony Mei

Can anyone explain how to use the normalizing factor? Thanks a lot.

Zhuo Feibao

Ahmed Bderhman

I think this is a good working and had help me more.

I have this question: i like to change euclidean distance{d=(repmat(Test',1,N)-repmat(Ref,M,1)).^2;} with mahalanobis distance is it possipole? if yes how can i do that?

because i like to test the efficiency of my system when i use euclidean distance and when i used mahalnobis distance.

Sai Man

Great code thanks for writing. But, I noticed a small discrepancy with my C code. In the .m file line 45:

it would be much better to have the D(n-1,m-1) element in the first argument like this:
and also swap the cases on the following lines.

This is because we are trying to find the minimum path length, and in the case where D(n-1,m-1)=D(n,m-1) or D(n-1,m-1)=D(n-1,m) then it would make sense to preference the diagonal element over the offdiagonal.

twinss ahmed

Pau Micó

I think it should be better to replace the sentence: w=cat(1,w,[n,m]); by: w=[n m; w];
In this way we get the w indexes in ascending order and it is easier to get t_warped and r_warped as: t_warped=t(w(:,1)); and r_warped=t(w(:,2));. In this way we can directly compare the warped sequences with the original ones without any index inversion.

weera kompreyaray

Sven Mensing

Good function
Another 10-fold Speed-up can be achived if you use a double-MIN construction for the Distance matrix


Boat MiMi


Yuzeng Lv

reza rezaww

Rogelio Ramos

Natan K

Can you tell me about speaker verification using mfcc and dtw. Please let me know.

jamal habibi

Ivo Ihrke

The loop

for m=2:m

contains an error ( for statement ), should be:

for m=2:M

If you want to compare vectors, where the vectors themselves are unknown, but their distance matrix is known, you can use your distance matrix instead of "d"

Georg Schmitz

The routine can be made 10 times faster by doing the d(n,m) calculation by


instead of the loop.

Carlos Franco

mahajan bhushan

zhlong l


Faster implementation thanks to review by Georg Schmitz.

Few minor updates

MATLAB Release Compatibility
Created with R12.1
Compatible with any release
Platform Compatibility
Windows macOS Linux