Going back in time 1 week or a year
조회 수: 7 (최근 30일)
이전 댓글 표시
Dear all,
I attach a data set of weekly observations on an index.
Suppose that I am in week 2023/6/16 (yellow cell). For this cell I want to obtain the corresponding index value
Then I want to go back one week (7 days) ; that is to 2023/6/9 (yellow cell) and calculate its corresponding index value and then calculate the percentage change between the two values.
I used something like
t = datetime(2023,6,16);
t2 = dateshift(t,'dayofweek', 7,'previous')
but it does not work. I am sure that there is a mistake in my code. Is there a way to do that.
Also, assuming that we are in week 2023/6/16, I want to calculate the mean, min, max of the index values for the last 12 months. Is there a way to do that?
I would very much appreciate some guidance.
Thank you in advance!
댓글 수: 0
채택된 답변
Voss
2023년 7월 14일
편집: Voss
2023년 7월 17일
T = readtable('data.xlsx')
"Suppose that I am in week 2023/6/16 (yellow cell). For this cell I want to obtain the corresponding index value
Then I want to go back one week (7 days) ; that is to 2023/6/9 (yellow cell) and calculate its corresponding index value and then calculate the percentage change between the two values."
dt = datetime(2023,6,9); % June 9th, 2023
row_dt = find(T.Date == dt);
row_a_week_before_dt = find(T.Date == dt - caldays(7));
Index_dt = T{row_dt,'Index'}
Index_a_week_before_dt = T{row_a_week_before_dt,'Index'}
percent_change = 100*(Index_dt-Index_a_week_before_dt)/Index_a_week_before_dt
"Also, assuming that we are in week 2023/6/16, I want to calculate the mean, min, max of the index values for the last 12 months. Is there a way to do that?"
dt = datetime(2023,6,16); % June 16th, 2023
row_dt = find(T.Date == dt);
row_a_year_before_dt = find(T.Date > dt - calyears(1), 1);
Index_last_year = T{row_a_year_before_dt:row_dt,'Index'};
mean_last_year = mean(Index_last_year)
min_last_year = min(Index_last_year)
max_last_year = max(Index_last_year)
댓글 수: 2
Peter Perkins
2023년 7월 17일
This answer is incorrect. Do NOT subtract days or years for this purpose.
As the doumentation says, these are "exact-length time" units equal to exactly 24hrs and 365.2425*24hrs. You may be using unzoned datetimes today, but eventually, you will use time zones, in which case days will give you the wrong answer.
years will already give you the wrong answer.
Use caldays and calyears, as Steve Lord says.
I also recommend that you use timetables, and look into things like groupsummary or retime, to compute data summaries over each week, or year or whatever. Your life will be much easier.
추가 답변 (1개)
Steven Lord
2023년 7월 15일
Subtract the appropriate calendar duration.
t = datetime('today')
oneWeekAgo = t - calweeks(1)
oneYearAgo = t - calyears(1)
Don't try to subtract 1 year as a duration. That's 365.24 days, not exactly 1 year.
oneYearAgoAlmost = t - years(1)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!