Going back in time 1 week or a year
15 次查看(过去 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!