How to interpolate missing value using n values before and after the missing data?
Show older comments
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 Comments
"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
on 11 Jan 2022
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
on 11 Jan 2022
Mathieu NOE
on 12 Jan 2022
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
on 12 Jan 2022
Read about the function fillmissing.
Jan Kudlacek
on 12 Jan 2022
Prashanti, can you please Accept my answer? :-)
John D'Errico
on 12 Jan 2022
Edited: John D'Errico
on 12 Jan 2022
@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
on 12 Jan 2022
@Mathieu NOE: it won't make any difference.
Answers (0)
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!