finding special date time

조회 수: 4 (최근 30일)
Abolfazl Nejatian
Abolfazl Nejatian 2019년 12월 11일
댓글: Abolfazl Nejatian 2019년 12월 12일
Hey everyone,
i working on the financial pattern recognition and i need to filter my extracted pattern by some date time method,
for instance, i need to preserve all patterns which occur on the same day in the entire dataset or preserve all pattern occur in the same month or in the same quarter of year.
(suppose that my dataset begins from 1-1-2017 till 1-1-2019 )
i think, if today is Sunday and i want to preserve all of Sunday in my dataset, the challenge is how to build a Date number Variable that contains all of Sundays from 1-1-2017 to 1-1-2019?! 
the second question is, in order to eliminate unuseful date from my DateTime Variable is there any work possible like logical indexing to speed up my code?
thanks in advanced
Abolfazl.

채택된 답변

Steven Lord
Steven Lord 2019년 12월 11일
Find the next Sunday after a given date.
T = datetime('today');
nextSun = dateshift(T, 'dayofweek', 'Sunday', 'next')
All Sundays between today (T) and the start of 2021.
start2021 = datetime(2021, 1, 1);
allSundays = nextSun:calweeks(1):start2021
Let's check.
[~, dayOfWeek] = weekday(allSundays)
% or
dName = day(allSundays, 'shortname')
dNum = day(allSundays, 'dayofweek')
Or if you already have a vector of datetime values, call day or weekday with one output (for day you'll want to specify 'dayofweek' as the kind) and extract those elements of your vector with day number 1 (Sunday.)
  댓글 수: 1
Abolfazl Nejatian
Abolfazl Nejatian 2019년 12월 12일
dear Steven,
thank you very much for your consideration.
i have find another way that insert it below,
nowDate = datetime('today'); % today Time
patternDate = datetime(2010,1,1):hours(.5):nowDate % let's suppose it is a time vector from 01/01/2000 with half hour interval
%---- finding the same month in our patternDate vector
Month = (patternDate.Month == nowDate.Month);
%---- finding the same weekday in our patternDate vector
WeekDay = weekday(patternDate) == weekday(nowDate);
%---- pattern that happened in same month and same day of week
patternDate(WeekDay & Month)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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