MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Replacing the missing values by interpolation

Asked by Mary on 2 Apr 2013

Hi, i have a data set (variable pc) of size 19944x1 split into 24 intervals. in each interval, some of the data points are missing (where it is equal to 0). Where the data points are missing, I am trying to interpolate between two valid points. if the interval starts with 0, i want to ignore it till it reaches a valid number (>1). and if the situation is as such: ps = [1 2 3 0 0 0 0 4 5 ...] i want to interpolate data between 3 and 4 such that the 4 zeros will be replaced by 4 different interpolated values.

i have written my code below: the variable count is the number of data points missing between each valid data.

for l = 1:24:19944

for j = 1:24

```        if ps(c)== 0
if j ~= 1
prevS = ps(c-1)
nextS = ps(c+1)
counter = counter + 1
c = c+1
if prevS == 0
display('not gonna do it');```
```                else %if prevS != 0
if nextS ~= 0
%%%what if n = 0
speed(d) = interp(prevS,nextS,counter)
d = d+1;
end
end
else
display('unaccessable')%%if i = 1
c = c+1
end %% end of i condition
else
speed(d) = ps(j);
end %%if ps = 0```
```    end% end of inner for
end %end of main for```

I am encountering problem with updating my matrix speed with the new values. Also, how can I code such that I can Any idea how I can do that? Any advice would be appreciated.

## Products

No products are associated with this question.

Answer by Val Cri on 2 Apr 2013

Why not removing the zeros from your arrays, then you reinterpolate to any new breakpoints you want. Here is an example:

a = [ 2 3 0 0 2 3 0 1]; index = [1 2 3 4 5 6 7 8]

not_zero = a~=0;

a_interpolated = interp1(index(not_zero),a(not_zero),index)