Repeating a time series from yyy-MM-dd to yyyy-MM-dd-hh-mm
1 次查看(过去 30 天)
显示 更早的评论
I have a matrix where the first column is a time series with dates down to one minute resolution, in the format: yyyy-MM-dd-hh-mm. And in the columns next to it I want to add several types of measured data.
The problem is that some of this data has a lower resolution, for example only measured once a day in the format: yyyy-MM-dd.
What I want to do is insert this data in the high resolution matrix and having the daily values repeated across all hours and minutes of the correct day so that there are no gaps between the values.
Example: I have carbon dioxide data from 2011 measured daily. I want the measured value for 2011-01-01 to be repeated in the bigger matrix all the way from 2011-01-01-00-00 to 2011-01-01-23-59 where then 2011-01-02 starts and so on..
I imagine this shouldn’t be too complicated to do but I’m having trouble figuring out how to do it. Any suggestions?
Thanks.
0 个评论
采纳的回答
Guillaume
2015-2-17
Actually, for this I would use date vectors. You can then use ismember with the 'rows' option to locate the matching rows of minute and daily data (using only the year month day columns of the date vectors):
%demo data, yours does not overlap:
DailyDatenum = [734504:734868; 1:365]';
MinuteDatenum = [734502:1/24/60:734870]';
Dailydv = datevec(DailyDatenum(:, 1));
Minutedv = datevec(MinuteDatenum(:, 1));
[ispresent, row] = ismember(Minutedv(:, 1:3), Dailydv(:, 1:3), 'rows');
MinuteDatenum(ispresent, 2) = DailyDatenum(row(ispresent), 2);
6 个评论
Guillaume
2015-2-20
Yes, I should have mentioned that the code assumes that there's no missing data at the beginning (for 'fillwithlast' and 'interpolate') and the end (for 'interpolate' only) of minutedata. Because, if there is, there's nothing to replicate / interpolate between.
You can deal with these special case anywhere within the for loop, in the case statements for example if you want different behaviour:
case 'fillwithlast'
if runstart == 0
%missing data at the beginning
filldata = minutedata(runend+1); %not sure what you want to do. This will fail if everything is NaN since runend+1 is then past the end
else
filldata = minutedata(runstart - 1);
end
case 'interpolate'
if runstart == 0
filldata = minutedata(runend+1); %maybe?
elseif runend == numel(minutedata)
filldata = minutedata(runstart-1); %maybe?
else
filldata = linspace(minutedata(runstart-1), minutedata(runend+1), runend-runstart+3);
filldata = filldata(2:end-1);
end
nantransitions is a temporary variable to help in finding the beginning and end of each run of NaN. Since isnan returns a vector of 0 and 1, (e.g. [0 0 1 1 1 0 1 1 ]), you know that you go from non-nan to nan when the diff is 1 and from nan to non-nan when the diff is -1. The rest of the diff is always 0. Therefore, you've got a transition whenever the diff is non-zero. I just flank it with 0 to make sure that the first transition is always non-nan to nan and the last nan to non-nan (and to make sure that nan at the beginning or end are caught). Hence odd transitions are non-nan to nan and even transitions are nan to non-nan.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Behavior and Psychophysics 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!