Use DTW to cluster Uneven size time series

3 views (last 30 days)
Amila
Amila on 20 Mar 2023
Answered: yamid on 2 Nov 2023
Dear friends ,
I want to use DTW to clustor un even size data time series
my below code is working
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,:));
end
end
But the problem is I'm trying to use it for my time series (raws) witch are not in same length. I alined them with a time and make a matrics somthing like data = rand(161,20); but the problem is due to un even size (time), some raws has NaN at the 'end' and the 'beginning;
could you kindly helped me ...
my dat is look like this
NaN NaN NaN 0.5652 ... 0.9338 NaN NaN NaN
NaN NaN 0.6514 0.6486 ... 0.8719 NaN NaN NaN
NaN NaN 0.4987 0.7981 ... 0.3011 0.8229 NaN NaN
NaN NaN 0.2845 0.2204 ... 0.2360 0.6886 0.8708 NaN
NaN NaN 0.8306 0.8579 ... 0.8316 0.6039 0.3528 0.7837
NaN 0.1909 0.8184 0.9047 ... 0.4378 0.3870 0.4002 0.6733
0.7114 0.4286 0.9382 0.2920 ... 0.5071 0.0655 0.5979 0.8407
0.7834 0.0145 3.2610e-04 0.7259 ... 0.1855 0.9986 0.9115 0.4209
0.6239 0.3253 0.6404 0.3394 ... 0.1515 0.6610 0.1330 0.7037
... ... ... ... ... ... ... ... ... ...
When I run the codes all the raws that have NaN value going to give NaN as clustor, how i can avoid this
please kindly help me
  1 Comment
Amila
Amila on 27 Mar 2023
If anyone has any idea on how to do it please give me some advice ,
Thank you !

Sign in to comment.

Answers (1)

yamid
yamid on 2 Nov 2023
Just make sure the samples in data are in rows, then I would define dtwf as below:
function dist = dtwf(x,y)
x_nonNan= x(~isnan(x));
y_nonNan= y(~isnan(y));
m2 = size(y,1);
dist = zeros(m2,1);
for i=1:m2
dist(i) = dtw(x_nonNan,y_nonNan(i,:));
end
Then I use hierarchical clustering
ncluster=3;
dist = pdist(data, @dtwf);
Z = linkage(dist, 'complete');
idx = cluster(Z, 'maxclust', nclusters);

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!