closest day a year back

2 visualizaciones (últimos 30 días)
joseph Frank
joseph Frank el 21 de Oct. de 2012
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..

Respuestas (2)

Star Strider
Star Strider el 21 de Oct. de 2012
Editada: Star Strider el 21 de Oct. de 2012
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 el 21 de Oct. de 2012
Editada: Azzi Abdelmalek el 21 de Oct. de 2012
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

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by