这是我关于如何使用“反向”插值来解决这个问题的想法(在通常我们想要找到y
对应于 some 的值的意义上是反向的x
,但这里是相反的):
function q51282667
%% Generate data:
x = linspace(1,100,1000);
y{1} = tanh(0.09*x) ;
y{2} = tanh(0.09*(x+10));
y{3} = tanh(0.09*(x-10));
% ^ y,y1,y2 are not necessarily the same length so I used a cell and not a numeric array
%% Find alignment:
% Establish a baseline: the curve with the largest vertical extent:
[~,mxi] = sort(cellfun(@max,y) - cellfun(@min,y), 'descend');
% Reverse interpolation using y-values:
ny = numel(y);
deltaX = zeros(ny,1);
for indY = 1:ny
deltaX(indY) = interp1(y{mxi(1)}, x, y{indY}(1)) - x(1);
end
%% Plot:
% Original:
figure(); plot(x, y{1}, x, y{2}, x, y{3}); % this is the same as your example
% Shifted:
figure(); plot(x + deltaX(mxi(1)), y{mxi(1)}, ...
x + deltaX(mxi(2)), y{mxi(2)}, ...
x + deltaX(mxi(3)), y{mxi(3)});
导致:
deltaX =
10.0000063787562
20.0000993310027
0
和: