Calculating week numbers in matlab

I have an annual time series where measurements are recorded at hourly intervals:

StartDate = '2011-01-01 00:00'; EndDate = '2011-12-031 23:00'; DateTime=datevec(datenum(StartDate,'yyyy-mm-dd HH:MM'):60/(60*24):... datenum(EndDate,'yyyy-mm-dd HH:MM')); dat = 2+(20-2).*rand(size(DateTime,1),1); 

I would like to calculate the average 24-hour cycle for each week of the year, that is, for week 1, day of the year 1 to 7, I want to calculate the average value of 00:00, 01:00, ... and so on, so in the end I I get a 52, 24-hour series, i.e. one for every week of the year. Matlab has a function called "weeknum" that returns the week number from a given seriel date number, however this function is in the financial toolbar. Can anyone suggest an alternative method for finding the week number?

+4
source share
4 answers

Maybe this might help (I use the current date and time as an example):

 c = datevec(datestr(now)); week_num = ceil(diff(datenum([c(1), 1, 1, 0, 0, 0; c])) / 7) 

I'm not sure how this solution handles edges correctly, but I think this is a good place to start.

You can also check it using this website , which reports the current week number.

Applying this to your example can be done, for example, like this:

 weeknum = @(v)ceil(diff(datenum([v(1), 1, 1, 0, 0, 0; v(:)'])) / 7); arrayfun(@(n)weeknum(DateTime(n, :)), 1:size(DateTime, 1))' 
+2
source

According to Wikipedia, the ISO week number is calculated like this .

I did it for today as an example.

 offsetNotLeap = [0 31 59 90 120 151 181 212 243 273 304 334]; offsetLeap = [0 31 60 91 121 152 182 213 244 274 305 335]; todayVector = datevec(today); todayNum = today; ordinalDay = offsetLeap(todayVector(2)) + todayVector(3); dayOfTheWeek = weekday(todayNum); weekNumber = fix((ordinalDay - dayOfTheWeek + 10)/ 7) weekNumber = 51 

I did not check for 0 and 53 cases. Also note that the MATLAB day of the week function gives the Sunday index as 1, so if you want to make the index on Monday 1, you need to make some adjustments. ISO says Monday should be 1.

0
source

Just a simple function of the day of the week from Monday to the first day of the week:

 function [ daynumber,dayname ] = weekd( date ) %WEEKD Returns the day number and day name based on the input date % First day of week % ----------------- % The first day of the week is Monday. % % Parameter % --------- % date = Input date -> format: dd.mm.yyyy / Example: 21.11.2015 [dn,dayname] = weekday(datenum(date,'dd.mm.yyyy')); if dn == 1; daynumber = 7; elseif dn >= 2 && dn <= 7 daynumber = dn - 1; else error('Invalid weekday number.'), end end 
0
source

although some time has passed since you posted your question, I want to give an additional answer if people have the same problem as mine. As HebeleHododo already pointed out, Wikipedia has a rule on how to calculate the ISO week number . But anyway, I could not find a common piece of code for it.

Therefore, I developed the following general method, which can take vectors, matrices, or individual values โ€‹โ€‹of arbitrary dates and times with arbitrary base years . We appreciate reading any constructive reviews. I successfully launched it on MATLAB R2017a and conducted further tests for some extreme cases.

 function week_num = get_week_num(dtimes) dnums = datenum(dtimes); firsts = datenum(year(dtimes), 1, 1); ordinal = floor(dnums - firsts); wday = weekday(dtimes) - 1; wday(wday == 0) = 7; week_num = floor((ordinal + wday + 10) / 7); end 
0
source

All Articles