Leap days and extracting data disregarding the year
Mostra commenti meno recenti
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
Risposte (0)
Categorie
Scopri di più su Calendar in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!