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 Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by