How to remove February 29th for leap years in a daily time series over 43 years?
51 views (last 30 days)
I have an array of values (x) representing daily data for 43 years (1979-2021) where size(x)=15706
I rearranged the data to have a matrix with years as rows and days as columns.
% some x --> keep only multiple of 365 days (removes last 11 days)
x = x(1:365*43).';
% reshape x to have years as rows and days as columns
x_matrix = reshape(x,365,).'
However, this is problematic for leap years since now my columns in x_matrix are not aligned with a given day. Indeed, x_matrix(year n, 1) is not always January 1 but is shifted toward later in January as years pass.
My data represents year 1979-2021 where 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020 are leap years.
How can I remove the last day of February for each leap year to have x_matrix align for each year with a given day where x_matrix(year n, 1) is always January 1?
Is there a useful function for that somewhere?
Jon on 1 Jun 2022
Edited: Jon on 1 Jun 2022
I fully support @dpb suggestions regarding working with timetables. However for purposes of making your graphic, you could do this
% define example data
x = rand(15706,1);
% make datetimes corresponding to data
t1 = datetime(1979,1,1);
t2 = datetime(2021,12,31);
t = t1:t2;
% find all the leap days and remove them
idl = month(t)==2 & day(t)==29;
xClean = x(~idl);
% make matrix with rows as days and years as columns
X = reshape(xClean,365,43)
More Answers (1)
James Tursa on 1 Jun 2022
Edited: James Tursa on 1 Jun 2022
To get rid of the leap days, you can use evenly spaced indexing since the number of days between leap days is constant for your time span. Since index 1 corresponds to Jan 1, 1979, that means the first leap day is Feb 29, 1980 which is index 365+31+29 = 425. The next leap day will be 4*365+1 = 1461 days later. So the code to get rid of the leap days for your data is (do this prior to reshaping):
x(425:1461:end) = ;