-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_1d.m
62 lines (52 loc) · 1.31 KB
/
test_1d.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
54
55
56
57
58
59
60
61
%%
% Test for 1-D optimal transport.
N = 40;
vmax = 20;
SetAR = @(ar)set(gca, 'PlotBoxAspectRatio', [1 ar 1]);
% generate Gaussian mixtures points in 1-D
x = 14 + 2*randn(N,1);
x = round(clamp(x,0,vmax));
y = 6 + 3*randn(N,1);
y = round(clamp(y,0,vmax));
ms = 30;
r = .2; E = 0*x + randn(N,1)*r;
% scatter plot
clf;
subplot(2,1,1);
plot(x, E, '.r', 'MarkerSize', ms); axis([0 vmax -.5 .5]); axis equal;
set(gca, 'YTick', []);
subplot(2,1,2);
plot(y, E, '.b', 'MarkerSize', ms); axis([0 vmax -.5 .5]); axis equal;
set(gca, 'YTick', []);
% histogram
subplot(2,1,1);
hx = hist(x,1:vmax);
bar(1:vmax,hx, 'r'); axis tight;
subplot(2,1,2);
hy = hist(y,1:vmax);
bar(1:vmax,hy, 'b'); axis tight;
%%
% animation on histograms
% sort
xs = sort(x);
ys = sort(y);
%
figure(2);
while true
for i=[1:K, K-1:-1:1]
t = (i-1)/(K-1);
z = (1-t)*xs+t*ys;
hz = hist(z,1:vmax);
clf;
subplot(3,1,1);
plot(z, E, '.', 'color', [1-t,0,t], 'MarkerSize', ms); axis([0 vmax -.5 .5]); axis equal;
title('Optimal transport');
subplot(3,1,2);
bar(1:vmax,hz, 'FaceColor', [1-t,0,t]); axis([0,vmax,0,8]);
title('Optimal transport histogram interpolation');
subplot(3,1,3);
bar(1:vmax,(1-t)*hx+t*hy, 'FaceColor', [1-t,0,t]); axis([0,vmax,0,8]);
title('Linear histogram interpolation');
drawnow;
end
end