closest day a year back

2 次查看(过去 30 天)
joseph Frank
joseph Frank 2012-10-21
Hi,
I have a vector of dates and my alogorithm is picking one observation X in a loop. I want to go back an exactly year back from the date of X. but the date sometimes doesn't exist because it is not a working a day and I am interested only in working days. How can I choose from this vector the closest working day which is X-365 but if it doesn't exist then it should be X-364 or X-363 etc..

回答(2 个)

Star Strider
Star Strider 2012-10-21
编辑:Star Strider 2012-10-21
Here's a simple routine that should work:
refdate = datenum([2012 10 16]); % Test Day1
refdate = datenum([2012 10 15]); % Test Day2
yearago = addtodate(refdate, -1, 'year');
[Nya, Sya] = weekday(yearago)
if Nya == 1
yearago = addtodate(yearago, 1, 'day');
elseif Nya == 7
yearago = addtodate(yearago, -1, 'day');
end
[NyaW, SyaW] = weekday(yearago)
The two refdate values put yearago on a Saturday or Sunday. The if block substitutes Friday for Saturday and Monday for Sunday. The yearago variable is the date number for that day.

Azzi Abdelmalek
Azzi Abdelmalek 2012-10-21
编辑:Azzi Abdelmalek 2012-10-21
use
[day_number,day]=weekday(datestr(now-365)) % date from now
or
dat=datenum('21-Sep-2012 21:36:20')
[day_number,day]=weekday(datestr(dat-365)) % date from any day
Now you have a number day, Sunday=1 Monday=2,... I don't know what do you mean by not working days, do you include days other then Sunday, like, Thanksgiving

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by