How to combine Matlab's build-in functions dtw and pdist?

20 visualizaciones (últimos 30 días)
DS
DS el 16 de Nov. de 2017
Comentada: Greg Dionne el 20 de Mzo. de 2023
Hi, I'm trying to perform hierarchical clustering on my data. I've tried several distance metrics, but now I would like to use the build-in function for dynamic time warping (Signal Processing Toolbox), by passing the function handle @dtw to the function pdist. Following problem occuried:
Error using pdist (line 391)
Error evaluating distance function 'dtw'.
Caused by:
Error using dtw (line 87)
The number of rows between X and Y must be equal when X and Y are matrices
Here is the code I'm using:
D = pdist(data,@dtw); %data is a 1184x38 double matrix, where 1184 is the number of time-series
Z = linkage(D,'ward');
res = cluster(Z, 'maxclust', numClusters); %e.g. numClusters = 5
Many thanks in advance!
  1 comentario
Dankur Mcgoo
Dankur Mcgoo el 5 de Jul. de 2018
Did you end up solving your problem? I am facing the same issue.

Iniciar sesión para comentar.

Respuestas (1)

Greg Dionne
Greg Dionne el 9 de Ag. de 2018
You'll want to take the output of DTW and put it into a form that PDIST can recognize.
This should get you started:
function d = dtwdist(Xi, Xj, varargin)
[m,n] = size(Xj);
% preallocate
d = zeros(m,1);
for j=1:m
d(j) = dtw(Xi, Xj(j,:), varargin{:});
end
Use it like:
X = randn(118,38);
maxsamp = 10;
d = pdist(X,@(Xi,Xj) dtwdist(Xi,Xj,maxsamp,'squared'))
imagesc(squareform(d));
colorbar;
title('Distance Matrix')
  3 comentarios
Greg Dionne
Greg Dionne el 20 de Mzo. de 2023
Hi Amila,
DTW will not work in the presence of NaN values.
Maybe if you give a little back-story on what each column represents? If all you need is to compute a distance metric between each column (without NaN), maybe you could filter them out before sending them to DTW?
If you have a vector, v, you can remove NaN values via:
v = v(~isnan(v));
For matrices, it's probably easier to operate on them one column/row at a time before sending to DTW.
row = M(j, :)
row = row(~isnan(row));
So (assuming I understood your question correctly) something like:
function d = dtwdist(Xi, Xj, varargin)
[m,n] = size(Xj);
% preallocate
d = zeros(m,1);
for j=1:m
x = Xi(~isnan(Xi));
y = Xj(j,:);
y = y(~isnan(y));
d(j) = dtw(x, y, varargin{:});
end
Hope that helps.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by