-
Notifications
You must be signed in to change notification settings - Fork 11
/
warping.m
53 lines (39 loc) · 972 Bytes
/
warping.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function [D, path] = warping(C)
% WARPING This function finds the optimal path for a given cost matrix C
[m, n] = size(C);
% adding zeros top and left of C
C = cat(1, zeros(1, n), C);
C = cat(2, zeros(m+1, 1), C);
% creating the cumulative cost matrix
D = zeros(size(C));
% filling it with prohibitive values on the borders
D(1, 2:end) = inf;
D(2:end, :) = inf;
for i = 1:m
for j = 1:n
[m, id] = min([D(i+1,j), D(i,j)]);
D(i+1, j+1) = C(i+1, j+1) + m;
end
end
D(1,:) = inf;
D(:, 1) = inf;
% starting at the end of the matrix
[i,j] = size(D);
path = [i,j];
% while not at the fake corner
while i>1 && j>1
% find the minimum
[~, id] = min([D(i-1,j-1), D(i,j-1)]);
if id == 1
i = i-1;
j = j-1;
elseif id == 2
j = j-1;
end
% add the min step to the path
path = cat(1, [i,j], path);
end
% remove last step, and decrement by one
path = path(2:end, :) - 1;
D = D(2:end, 2:end);
end