Looping interpolation scheme has errors for some (but not all) datasets?
Show older comments
Hello,
I'm having trouble with an interpolation function I'm trying to write, for use with multiple large datasets. For each dataset, I will have a given time period and two time series: 'a', with data taken at 64-second intervals, and 'b', with data taken at 60-second intervals. I want to resample the 'a' dataset to have the same length as the 'b' dataset, using this code:
resampled_a_data = 1:1:length(b_data);
a_timestamps = 0:64:(64*length(a_data));
timestamp = 0;
for i = resampled_a_data
%find index j of closest value in a_timestamps
j = interp1(a_timestamps,1:length(a_timestamps),timestamp,'nearest');
a_datapoint = a_data(j);
%replace resampled_a_data[i] with a_data[j]
resampled_a_data(i) = a_datapoint;
timestamp = timestamp + 60;
end
I have tried this code with three datasets so far. One of them works perfectly well, but for the other two datasets, the scheme gives an error at
a_datapoint = a_data(j);
when i is somewhere in the 40000s (it is different for both datasets), and for all i after that datapoint. Prior to giving an error, the contents of the array resampled_a_data are exactly what I would expect them to be. Can anyone advise me on how to fix this error, or alternatively, a better way to accomplish the same goal of resampling the datasets?
Accepted Answer
More Answers (0)
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!