Split a target date interval into seasons and find the percentile of days for each season
9 次查看(过去 30 天)
显示 更早的评论
I have the following seasons:
- Spring: {01-March , 31-May}
- Summer: {01-June, 31-August}
- Autumn: {01-Sep, 30-Nov}
- Winter: {01-Dec, 28-Feb}
and I also have as a data a reference period, e.g. 01-Jan until 15-Sep (254 days).
What I want is to compute what is the percentile of the days of reference period that belong to each season.
For example, in the present case, we have:
- Winter: 01-Jan until 28-Feb --> 59 days / 254 days = 23.5%
- Spring: 01-Mar until 31-May --> 90 days / 254 days = 35%
- Summer: 01-Jun until 31-Aug --> 90 days / 254 days = 35%
- Autumn: 01-Sep until 15-Sep --> 15 days /254 days = 6.5%
So the requested values would be {23.5, 35, 35, 6.5}.
0 个评论
采纳的回答
Scott MacKenzie
2021-7-11
There might be a way to shorten this, but I think it achieves what you are after. You didn't mention the year, so I set this up as a variable (change, as necessary). This script accommodates leap years. BTW, the number of reference days below is 259. Not sure why this differs from your count of 254.
yr = 2020; % change, as necessary
% reference days of interest
r1= datetime(yr,1,1):datetime(yr,2,eomday(yr,2));
r2= datetime(yr,3,1):datetime(yr,5,31);
r3= datetime(yr,6,1):datetime(yr,8,31);
r4= datetime(yr,9,1):datetime(yr,9,15);
n = length([r1 r2 r3 r4]) % number of reference days
percentDays = [length(r1), length(r2), length(r3), length(r4)] /n * 100
4 个评论
Scott MacKenzie
2021-7-11
@DIMITRIS GEORGIADIS You're welcome. Glad to help. Concerning your first question, you can't really get rid of the yr variable because of leap years. If don't care about Feb 29, then sure. Just substitute any year (e.g., 1) for yr in the script.
In terms of generalizing this, sure that can be done. You could write a function that receives as input the month and day of the begining of each reference period. You'd also need the month and day of end of the last reference period. The function would return the percentages, as in my example script. To shorten the code and allow for any number of reference periods (in case you somtimes need >4), you could use structures or arrays to hold the month+day inputs and the percentages output.
更多回答(2 个)
Seth Furman
2021-7-14
To add to Scott's answer, this kind of grouped calculation on timestamped data lends itself well to timetable and groupsummary.
isWinter = @(dt) dt.Month == 12 | dt.Month <= 2;
isSpring = @(dt) 3 <= dt.Month & dt.Month <= 5;
isSummer = @(dt) 6 <= dt.Month & dt.Month <= 8;
isAutumn = @(dt) 9 <= dt.Month & dt.Month <= 11;
referencePeriod = timetable('RowTimes',datetime(2020,1,1):caldays(1):datetime(2020,9,15));
referencePeriod.Season(isWinter(referencePeriod.Time)) = categorical("Winter");
referencePeriod.Season(isSpring(referencePeriod.Time)) = categorical("Spring");
referencePeriod.Season(isSummer(referencePeriod.Time)) = categorical("Summer");
referencePeriod.Season(isAutumn(referencePeriod.Time)) = categorical("Autumn");
counts = groupsummary(referencePeriod,"Season")
counts.Percentages = counts.GroupCount ./ sum(counts.GroupCount)
2 个评论
Peter Perkins
2022-4-11
The general idea in Seth's suggestion is a good one, and his solution works for multiple years of data. If the data are always within one year, discretize would make this even simpler. Obviously winter crosses a year boundary, but it's not hard to use discretize in a way that handles that:
dt = datetime(2022,1,1):datetime(2022,9,15);
edges = datetime(2022,[1,3,6,9,12,13],[1,1,1,1,1,1]); % winter at both ends
season = discretize(dt,edges,"categorical",["Winter" "Spring" "Summer" "Autumn" "Winter"])
summary(season)
Using groupsummary gives you a nice output table; creating season would work as a lead-up to that too.
Peter Perkins
2021-7-27
Another possibility:
>> yr = 2020;
>> edges = datetime(yr,[1 3 6 9 12,12],[1 1 1 1 1 32])
edges =
1×6 datetime array
01-Jan-2020 01-Mar-2020 01-Jun-2020 01-Sep-2020 01-Dec-2020 01-Jan-2021
>> t = datetime(yr,1,1:259)';
>> tf = isbetween(t,edges(1:end-1),edges(2:end),'openright'); % uses implicit expansion
>> tf(:,1) = tf(:,1) + tf(:,end);
>> tf(:,end) = [];
>> 100*sum(tf,1)/length(t)
ans =
23.166 35.521 35.521 5.7915
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!