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