Calculate difference from a time series
Show older comments
Hello all, i would appreciate your help to solve the following issue (avoiding unnecessary loops if possible). I have a time series of temperature with 4 daily values corresponding in times 0, 6, 12 and 18. I would like to create a new one where i will keep the day and the difference between the value temperature in 12 hr and 6 hr.
My dataset looks like
2007 1 1 0 5
2007 1 1 6 7
2007 1 1 12 14
2007 1 1 18 11
2007 1 2 0 8
2007 1 2 6 9
2007 1 2 12 12
2007 1 2 18 13
where first column is year, second month, third day, fourth time and sixth temperature. The new dataset i want to have will look like
2007 1 1 7
2007 1 2 3
Thank you in advance for your help
Accepted Answer
More Answers (1)
Guillaume
on 8 Jul 2015
I would do it like this:
temps = [
2007 1 1 0 5
2007 1 1 6 7
2007 1 1 12 14
2007 1 1 18 11
2007 1 2 0 8
2007 1 2 6 9
2007 1 2 12 12
2007 1 2 18 13];
[ymd, ~, idx] = unique(temps(:, 1:3), 'rows');
tempperday = nan(max(idx), 4);
tempperday(sub2ind(size(tempperday), idx, temps(:, 4)/6 + 1)) = temps(:, 5)
The tempperday array is pretty much the same data as temps but each row is a single day, and the columns are temperature at 0, 6, 8, 12 hours. Therefore to get the difference between 12 and 6:
diffperday = tempperday(:, 3) - tempperday(:, 2)
And to attach the day to it:
newtemps = [ymd diffperday]
Categories
Find more on Smoothing and Denoising 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!