|
"Ryan Utz" <rutz@al.umces.edu> wrote in message <h4fqji$qu7$1@fred.mathworks.com>...
> I have a time series that consists of a continuous variable that occasionally has some missing data. The format of the data appears below:
>
> 1 0.5
> 2 0.6
> 3 0.6
> 4 0.7
> 5 -1
> 6 -1
> 7 -1
> 8 0.9
> 9 0.9
> 10 0.9
> 11 1.0
>
> The first column is simply an observation number tally; the second is the data of interest. Where a -1 occurs is missing data. I am trying to do a simple linear interpolation based on the points before and after the data go missing. For instance, for observation 5, the formula I wish to use is 0.7+(0.9-0.7)/(8-4). Thus observation 5 is 0.75. Observation 6 would be 0.75+(0.9-0.7)/(8-4), and so on until the data are no longer missing. Thus the second part of the expression (0.9-0.7)/(8-4) stays consistent for each instance of missing data (there are many, the one above is simply some example data).
>
> I'm a novice and am having some trouble getting my head around how to do this in a for-loop. Does anyone have any ideas or functions that may help with this endeavor?
>
> Many thanks ahead of time,
> Ryan
one of the many solutions
% the data
m=[
1 0.5
2 0.6
3 0.6
4 0.7
5 -1
6 -1
7 -1
8 0.9
9 0.9
10 0.9
11 1
];
% the engine
t=m(m(:,2)~=-1,:);
xi=m(:,1);
r=interp1(t(:,1),t(:,2),xi);
% the result
line([xi,xi],[m(:,2),r],'marker','s');
legend({'raw','interpolated'},'location','southeast');
us
|