How do I replace zero elements of a Time-series with the average of non-zero elements in it's vicinity?
    2 views (last 30 days)
  
       Show older comments
    
    Saeed Mohammadian
 on 4 Apr 2019
  
    
    
    
    
    Commented: Saeed Mohammadian
 on 5 Apr 2019
            I have a time-series traffic data which is the average speed of vehicles passing a fixed-point detector in every one-minute. It's possible that when no vehicle pass this decetor in any interval, the resulting speed is zero in this time series. However, from a traffic point of view, traffic states in these point should be considered as if a few vehicles were passing a free road. Therefore, I'm looking for a practical way to replace these zero elements with reasonable values in it's vicinity WITHOUT changing non-zero elements all over the time-series. In other words, I want to replace ONLY zero elements with an interpolation of the values in its vicinity. This means using filter function is not the case because: 
1) it will filter all other elemetns
2) it still does not change zero elements to the values of it's vicinity. 
Is there any practical function for this purpsose? Or I have to manually go through all these elements?
My time series looks like below:

0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 5 Apr 2019
        Change the 0 to nan, and then call John D'Errico's inpaint_nans https://www.mathworks.com/matlabcentral/fileexchange/4551-inpaint_nans
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
