special kind of interpolation
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Thorsten
2015-5-27
编辑:Thorsten
2015-5-27
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
更多回答(1 个)
Walter Roberson
2015-5-27
Search the data for 0's. Suppose you find it at index J. Then introduce a new timepoint just after t(J-1), at time t(J-1)*(1+eps), with value 0, and introduce another just before t(J+1), at time t(J+1)*(1-eps), again with value 0. Now that I think of it, you might as well change the time t(J) to be t(J-1)*(1+eps) to move the 0 to right beside the previous point, and then you would only need to insert one new point.
Doing the stitching together in vectorized form could be a bit tricky. Mumble...
tidx = sort([(1:length(t)).', find(datapoints(:)==0)]);
newt = t(tidx);
newdata = datapoints(tidx);
new_at = find(diff(tidx)==0);
newt(new_at) = newt(new_at-1)*(1+eps);
newt(new_at+1) = newt(new_at+2)*(1-eps);
newdata(new_at) = 0;
newdata(new_at+1) = 0;
There, that should be pretty close.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!