function distance = dtw(r,t)
[height width]=size(r);
[height2 width2] = size(t);
sum = 0;
sum2 = 0;
for i=1:height2
for j=1:height
for k=1:width2
sum = sum + (abs(t(i,k)-r(j,k)));
end
local_dist(i,j) = sum;
sum = 0;
end
end
[height3 width3] = size(local_dist);
accum_dist(1,1) = local_dist(1,1);
for x = 2:width3
accum_dist(1,x) = accum_dist(1,x-1)+local_dist(1,x);
end
for y = 2:height3
accum_dist(y,1) = accum_dist(y-1,1)+local_dist(y,1);
end
for a = 2:height3
for b = 2:width3
accum_dist(a,b) = local_dist(a,b) + min([accum_dist(a-1,b) accum_dist(a,b-1) accum_dist(a-1,b-1)]);
end
end
distance = accum_dist(height3, width3);