MATLAB Answers

How can I find all zeros in a vector and change those values by interpolating the last number before zero and the first number after zero ?

49 views (last 30 days)
PIRIL on 20 Feb 2014
Commented: Lee on 2 Feb 2018
Hi all,
I have a vector of data including zeros. I want to find and delete all zeros and interpolate the missing values.
For instance:
A=[123 443 652 222 0 0 0 0 0 0 312 5 6]
In this case, I want to interpolate between 2 and 3.
Since I am quite new to MATLAB, I don't know how to do that.
Many thanks.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 Feb 2014
One way is to use interp1():
A=[123 443 652 222 0 0 0 0 0 0 312 5 6]
x = 1:length(A)
xi = 1:length(A)
zs = A==0 % Zeros locations
A(zs) = []
output = interp1(x, A, xi)

Sign in to comment.

More Answers (1)

Matt J
Matt J on 20 Feb 2014
There are lots of inpainting routines on the File Exchange
Most of them assume that your missing data is represented by NaNs, so you would first have to convert your zeros to NaNs,

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!