MATLAB: Special kind of interpolation

interp1

I have some data shown in the example figure below for which I want to perform an interpolation to calculate the average value. However, I need a linear interpolation between the datapoints, except for points that are zero, there are values should be put at zero between the two surrounding points. How can I achieve this?

Best Answer

This "interpolation" is basically a plot with values added where y is zero:
x = x(:)'; y = y(:)'; % force row vectors
ind = find(diff(y == 0) == -1); % find the index where y changes from zero to non-zero
% add points before and after ind with zero y value
for i = 1:numel(ind)
indi = ind(i);
x = [x(1:indi-1) x(indi-1) x(indi) x(indi+1) x(indi+1:end)];
y = [y(1:indi-1) 0 y(indi) 0 y(indi+1:end)];
end
plot(x, y) % plot the curve
Related Question