Divide time array in day and night

30视图(30天)
艾玛·布兰肯(Emma Blanken)
编辑: 乔纳斯 on 30 Jun 2018
Hello there,
目前,我试图将一个小时的时间划分为一小时(01:00 02-07-07-2014至01:00 02-07-07-07-2016,17545行)到白天和黑夜,即两个子阵列。
Thus, all the moments between 20.00 - 8.00 are night, and between 8.00 - 20.00 are day. Thus the first, 7 hours of the array should be counted as night, and from there on it should just 'select' the following 12 hours, put it in the day subarray, then the following 12 hours and put it in the night array and so on until the end (where the last 5 points should be put in the night array again).
Is there a way to do this? Thanks in advance (:

答案(2)

Shantanu Gontia
Shantanu Gontia on 30 Jun 2018
MATLAB提供 约会时间 处理日历信息的格式。您可以初始化 约会时间 通过提供初始日期和最终日期和使用来阵列 duration object of 1 hour to provide the step-size . Here is a sample snippet of code to perform this.
first = datetime('01:00 02-07-2014',“ inputformat','hh:mm dd-MM-yyyy');%获得初次约会
last = datetime('01:00 02-07-2016',“ inputformat','hh:mm dd-MM-yyyy');%获得最终约会
dates = first:hours(1):last;% Create an array of dates separated by 1 hour
功能 小时(1) produces a duration object of 1 hour. Now, you can index the array dates 使用 Hour field.
daytime_dates = dates(dates.Hour < 20 && dates.Hour >= 8);
daytime_dates will contain the required dates.
2 Comments
Shantanu Gontia
Shantanu Gontia on 30 Jun 2018
是的,如果您有相同时间的温度数据,则可以使用相同的条件 dates.Hour < 20 && dates.Hour >= 8 , to index the variable holding the temperature. This will give you the daytime temperatures.

Sign in to comment.


乔纳斯
乔纳斯 on 29 Jun 2018
编辑:乔纳斯 on 30 Jun 2018
有很多方法可以做到这一点。DateTime格式非常微不足道
%This is your time vector
time=datetime('2014-2-7 01:00')+hours(1:17545)';
%Extract rows where the hour-of-day is between 8 and 20
day=time(hour(time)>=8 & hour(time)<=20)
%提取行日的时间在20到8之间
night=time(hour(time)<8 | hour(time)>20)
如果您与每个时间插槽相关的多个变量,那么我建议您在一个 timetable :
TT=timetable(time,var1,var2,var3...)
时间表还可以使用DateTime格式,因此您仍然可以执行上述操作。如果您拥有多变量数据集,您会发现它非常有用。

Community Treasure Hunt

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

开始狩猎!