Leap days and extracting data disregarding the year

3 次查看(过去 30 天)
I have an hourly timeseries of windspeed over several years, with some gaps filled by NaNs. I want to fill the gaps with a day of data from another (randomly chosen) year. I have a random variable (Rand8H) which has a random value from 1 to 8 for each day, from which to choose the year. I have extracted the data for each year into a cell array (AM_ff_years). I am trying to use the DateTime timeseries for my data but with the year set to 0001 (DateTime_DaysTime) to compare with a standard year the same length of the (non-leap year) extracted years (days_num) to pull out the correct data from the extracted year data. I have used dates (not datenum) to try and get around the issue of datenum not equalling the same actual date during leap years, but I still think the leap year is not accounted for properly. This also means the code is not running through because using actual dates in an if == is superbly slow. I have tried to run it with datenum but then I get an 'index exceeds matrix dimensions' error. Anyone got any ideas? I feel a bit lost with this!
%Now need to create a years worth of randomly chosen days based on the
%day/month
Rand8=randi([1 8],NHours/24,1); %Random number between 1 and 8 for each whole day
AM_ff_rand = NaN(NHours,1);
Rand8H=NaN(NHours,1);
%Let's use remat to sort Rand into days
Rand8rep=repmat(Rand8',24,1); %Creates 24 rows of each
Rand8H=reshape(Rand8rep,[NHours, 1]); %Creates series of random numbers, each the same for the hour of each day
%Create cell array of years
AM_ff_years={1,2,3,4,5,6,7,8;AM_ff_2005,AM_ff_2006,AM_ff_2007,AM_ff_2008,AM_ff_2009,AM_ff_2010,AM_ff_2011,AM_ff_2012}; %Create cell array of all the years
d1=datetime(0001,1,1,0,0,0); d2=datetime(0001,12,31,23,0,0);%Making it year 1 so not a leap year
days=(d1:hours(1):d2)';
days_num=datenum(days);
DateTime_DaysTime=datetime(0001,month,DateTime_Day2,hour,0,0);%This is the day and time but no year
DateTime_DaysTime_num=datenum(DateTime_DaysTime);
for t=1:NHours
for d=1:8760
if DateTime_DaysTime(t)== days(d)%So the date and time match, disregarding the year
AM_ff_rand(t)= AM_ff_years{2,Rand8H(t)}(d); %Accesses the correct year based on the random choice
end
end
end
  1 个评论
Catriona Fyffe
Catriona Fyffe 2020-6-4
I am half way there...needed to have different rules for leap and non-leap years, but still need to figure out the rest, maybe in the morning ;)
for t=1:NHours
if (Rand8H(t)== 4 || Rand8H(t)== 8) && (DateTime_Year(t)==2008 || DateTime_Year(t)==2012) %so is a leap yearmin both random and actual time
for d=1:8784
if DateTime_DaysTime_leap_num(t)== days_num_leap(d)%So the date and time match, disregarding the year
AM_ff_rand(t)= AM_ff_years{2,Rand8H(t)}(d); %Accesses the correct year based on the random choice **Use curly brackets to get to the year in the cell then smooth brackets to get the index**
end
end
elseif (Rand8H(t)~= 4 && Rand8H(t)~= 8) && (DateTime_Year(t)~=2008 && DateTime_Year(t)~=2012) %so is not a leap year in both random and actual time
for e=1:8760
if DateTime_DaysTime_num(t)== days_num(e)%So the date and time match, disregarding the year
AM_ff_rand(t)= AM_ff_years{2,Rand8H(t)}(e); %Accesses the correct year based on the random choice **Use curly brackets to get to the year in the cell then smooth brackets to get the index**
end
end
elseif (Rand8H(t)== 4 || Rand8H(t)== 8) && (DateTime_Year(t)~=2008 && DateTime_Year(t)~=2012) %random time is a leap year but actual time is not
AM_ff_rand(t)=NaN; %For just now!!
else
AM_ff_rand(t)=NaN; %For just now!!
%Some combination of the above
end
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by