假设您有一系列 X/Y 值,其中存在“间隙”......
resolution = 0.1;
x = [0:resolution:10 10.5:resolution:20]; % 4 missing values (10.1, 10.2, 10.3, 10.4)
y = ones(1, length(x));
您如何识别序列中的“间隙”并将其替换为某种类型的值(例如,将其替换为 0)?
我认为这可行,但我想知道是否有更好的方法。此外,此方法仅适用于 1 个间隙(而不是多个间隙)。我希望有一种更简单的方法……甚至可能是一种非循环方法。
xDiff = diff(x);
calcResolution = min(xDiff); % Try to calculate original resolution
newY = y;
newX = x;
thresh = 0.000001;
for i=1:length(xDiff)
% Check any time the difference is larger than our resolution...
if (abs(xDiff(i) - calcResolution) > thresh)
gapSize = (xDiff(i) / calcResolution) - 1;
newY = [y(1:i) zeros(1, gapSize) y(i:end)];
newX = [x(1:i) (x(i) + calcResolution):calcResolution:x(i+1) x((i+1):end)];
end
end
% newX == 0:resolution:20
% newY == [1 1 1 1 1 1 1 ... 0 0 0 0 ... 1 1 1 1 1 1 1]