Filling in gaps incrementally

10 views (last 30 days)
I have a set of time points (first column) and associated coordinates (second column) as following (for illustrative purposes):
data = [1 2 3 4 5 6 7 8 9 10; 263 264 0 0 0 0 0 0 0 731]'
A value of 0 in the coordinate column means that my tracker could not accurately capture the location of my marker. Currently, I replace the 0s with the previous value as follows (with column 1 for time, 2-3-4 are X-Y-Z coordinates and column five = -1 if tracker could not locate marker):
sz = size(data(:,1),1);
for i = 1:sz
if data(i,5) == -1
data(i,2) = data(i-1,2);
data(i,3) = data(i-1,2);
data(i,4) = data(i-1,4);
end
end
Using the example above, I would end up with
data = [1 2 3 4 5 6 7 8 9 10; 263 264 264 264 264 264 264 264 264 731]'
However, this means that when I calculate the euclidian distance ( using sqrt((x(i)-x(i-1))^2+(y(i)-y(i-1))^2+(z(i)-z(i-1))^2) ), I end up with artificially elevated distances (using the example above) between the 9th and 10th measurement because I jump from 264 to 731.
What I would like to do is replace the 0s such that 264 --> 731 is reached uniformly. Using the example above, I want to calculate the difference between 10th and 2nd value, divide this by the number of cells I need to fill and add this number to (i-1). I've explained terribly, so using the example above:
(731-264)/7 = 66.7
data = [1 2 3 4 5 6 7 8 9 10; 263 264 330.7 397.4 464.1 530.8 597.5 664.3 731]'
My dataset is 80000 rows long and the 0s can occur randomly (except at the beginning) for various lengths (could have 3 zeros in a row, could also have 50). Any idea how I could code this? Any advice would be greatly appreciated.

Accepted Answer

Honglei Chen
Honglei Chen on 15 Mar 2018
Do you always have a fully populated first column as the time vector? If so, you can try the following to do the linear interpolation for you
nidx = data(:,2)==0;
data(nidx,2) = interp1(data(~nidx,1),data(~nidx,2),data(nidx,1),'linear','extrap')
HTH
  1 Comment
Nicolas Darmanthe
Nicolas Darmanthe on 15 Mar 2018
I'm doing some testing but I'm really liking your approach - very straight forward! Thank you!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!