How to interpolate missing value using n values before and after the missing data?
이전 댓글 표시
Hello All,
I am trying to linearly interpolate a missing value by using values that were measured before and after the missing data. However, I don't want to use the first value measured before and after the missing data. Rather I would like use n values before and after the missing data for linear interpolation. How can I do that?
Here is an example of my problem:
I want to interpolate for value between x(5) and x(7). But I want to use n = 3 values before and after x(6) for linear interpolation. I.e. I want to use x(3), x(4), x(5) and x(7), x(8), x(9) values for interpolation.
x = [1 2 3 4 5 7 8 9 10];
Thanks,
Prashanti
댓글 수: 8
"I want to use x(3), x(4), x(5) and x(7), x(8), x(9)"
What does it mean to use six points for linear interpolation?
Please show a reference of 1D linear interpolation that uses more than two points.
It sounds like you are attempting some kind of sliding-window smoothing.
Jan Kudlacek
2022년 1월 11일
Hi Prashanti, I think interp1 may be what you are looking for. Let me know if this helps. Jan
close all
clear
x = [1 2 3 4 5 7 8 9 10]'; % x coordinate
y = [0 0 1 1 1 4 8 16 32]'; % y coordinate
% subplot(311)
plot(x, y, 'k-o')
hold on
xq = 6; % query point
n = 3; % number of data points to use in interpolation
yqLinear = interp1(x(xq-n : xq+n-1), y(xq-n : xq+n-1), xq, 'linear'); % linear interpolation at the query point (ignores all but neighbouring points)
xLinear = [x; xq]; % concatenate the query point
yLinear = [y; yqLinear]; % concatenate the computed interpolated data point
xyLinear = sortrows([xLinear, yLinear]); % sort the array according to x coordinate (first column)
% subplot(312)
plot(xyLinear(:, 1), xyLinear(:, 2), 'r-x')
% Do the same with different interpolation method
xq = 6; % query point
n = 3; % number of data points to use in interpolation
yqPchip = interp1(x(xq-n : xq+n-1), y(xq-n : xq+n-1), xq, 'pchip'); % shape-preserving piecewise cubic interpolation (see help)
xPchip = [x; xq]; % concatenate the query point
yPchip = [y; yqPchip]; % concatenate the computed interpolated data point
xyPchip = sortrows([xPchip, yPchip]); % sort the array according to x coordinate (first column)
% subplot(312)
plot(xyPchip(:, 1), xyPchip(:, 2), 'b-*')
legend({'Original data', 'Linear interpolation', 'Cubic interpolation'})
Prashanti Ganesh
2022년 1월 11일
Mathieu NOE
2022년 1월 12일
hello
just for fun I tested this code against the regular "full range" interpolation - would give the same result (magenta diamonds vs blue curve) but that may be specific to the data used in this demo

close all
clear
x = [1 2 3 4 5 7 8 9 10]'; % x coordinate
y = [0 0 1 1 1 4 8 16 32]'; % y coordinate
yqPchip_direct = interp1(x, y, (1:10), 'pchip'); % direct - no "buffer" method
% subplot(311)
plot(x, y, 'k-o')
hold on
xq = 6; % query point
n = 3; % number of data points to use in interpolation
yqLinear = interp1(x(xq-n : xq+n-1), y(xq-n : xq+n-1), xq, 'linear'); % linear interpolation at the query point (ignores all but neighbouring points)
xLinear = [x; xq]; % concatenate the query point
yLinear = [y; yqLinear]; % concatenate the computed interpolated data point
xyLinear = sortrows([xLinear, yLinear]); % sort the array according to x coordinate (first column)
% subplot(312)
plot(xyLinear(:, 1), xyLinear(:, 2), 'r-x')
% Do the same with different interpolation method
xq = 6; % query point
n = 3; % number of data points to use in interpolation
yqPchip = interp1(x(xq-n : xq+n-1), y(xq-n : xq+n-1), xq, 'pchip'); % shape-preserving piecewise cubic interpolation (see help)
xPchip = [x; xq]; % concatenate the query point
yPchip = [y; yqPchip]; % concatenate the computed interpolated data point
xyPchip = sortrows([xPchip, yPchip]); % sort the array according to x coordinate (first column)
% subplot(312)
plot(xyPchip(:, 1), xyPchip(:, 2), 'b-*')
plot((1:10), yqPchip_direct, 'md')
legend({'Original data', 'Linear interpolation', 'Cubic interpolation', 'Cubic interpolation (direct)'})
KSSV
2022년 1월 12일
Read about the function fillmissing.
Jan Kudlacek
2022년 1월 12일
Prashanti, can you please Accept my answer? :-)
John D'Errico
2022년 1월 12일
편집: John D'Errico
2022년 1월 12일
@Jan Kudlacek - you CANNOT accept a comment as an answer. You never posted an answer, just a comment. If you think your comment deserves an acceptance, then you need to post it as an answer. We cannot even upvote comments.
Stephen23
2022년 1월 12일
@Mathieu NOE: it won't make any difference.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Interpolation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!