Clustering Time Series with DTW

112 views (last 30 days)
Hi everyone.
I have ~161 time series of heart rates taken during a vocalization. I would like to sort these using the DTW algorithm. I have tried using the following to do this:
[idx,c,sumd,d] = kmedoids(dat,nclust,'Distance',@dtw);
But I end up with the following errors. I have done this before... with the exact same code. Does anyone know what I could be doing wrong?
Error using pdist (line 371)
Error evaluating distance function 'dtw'.
Error in internal.stats.kmedoidsDistObj/pdist (line 65)
out = pdist(X,distObj.distance);
Error in kmedoids>precalcDistance (line 575)
distVec = distObj.pdist(X);
Error in kmedoids>loopBody (line 546)
xDist = precalcDistance(X,distObj);
Error in kmedoids (line 362)
oneRun = loopBody(algorithm,initialize,X,k,distObj,pneighbors,numsamples,options,display,usePool,onlinePhase);
Error in dtwclassifier (line 9)
[idx,c,sumd,d] = kmedoids(dat,nclust,'Distance',@dtw);
Caused by:
Error using dtw (line 115)
The number of rows between X and Y must be equal when X and Y are matrices
Would it be correct to compute my own distance matrix using DTW, and perform K-Means clustering on that?
Any help would be absolutely amazing! Thank you!

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 17 Feb 2021
I think the error is due the reason that dtw function operates on 2 signals only and the output is always a scalar.
As per the description of 'Distance' Name Value Pair Argument of the kmedoids function:
"See pdist for the definition of each distance metric. kmedoids supports all distance metrics supported by pdist."
And as per the description of Distance input argument of the pdist function for custom distance:
Custom distance function handle. A distance function has the form
function D2 = distfun(ZI,ZJ)
% calculation of distance
  • ZI is a 1-by-n vector containing a single observation.
  • ZJ is an m2-by-n matrix containing multiple observations. distfun must accept a matrix ZJ with an arbitrary number of observations.
  • D2 is an m2-by-1 vector of distances, and D2(k) is the distance between observations ZI and ZJ(k,:)."
Hence you can't use the dtw function handle directly and you can use it as follows:
data = rand(161,20);
[idx,c,sumd,d] = kmedoids(data,10,'Distance',@dtwf);
function dist = dtwf(x,y)
% n = numel(x);
m2 = size(y,1);
dist = zeros(m2,1);
for i=1:m2
dist(i) = dtw(x,y(i,:));
  1 Comment
Manash Sahoo
Manash Sahoo on 17 Feb 2021
This is fantastic! Thank you so much.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!