closest day a year back

2 visualizzazioni (ultimi 30 giorni)
joseph Frank
joseph Frank il 21 Ott 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..

Risposte (2)

Star Strider
Star Strider il 21 Ott 2012
Modificato: Star Strider il 21 Ott 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 il 21 Ott 2012
Modificato: Azzi Abdelmalek il 21 Ott 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

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by