Calculating Discharge Over Multiple Years
1 次查看(过去 30 天)
显示 更早的评论
I have a time series dataset which consists of the year in column 1 from 1883 - 2018, in column 2 there is month data and in column 3 each day. In column 4 there is a discharge value for each specific day. I need to calculate the maximum discharge for each year.
I am new to matlab and any help would be greatly appreciated
Thanks!
0 个评论
采纳的回答
Adam Danz
2020-1-2
编辑:Adam Danz
2020-1-3
The first 3 lines re-create the input matrix named m. See inline comments to understand the rest.
It produces a table T summarizing the results.
% recreate input data (m)
d = datetime(1883,9,1) : days(1) : datetime(1950,12,31);
[yr,mo,dy] = datevec(d);
m = [yr(:),mo(:),dy(:),rand(size(yr(:)))*50+40];
% Identify winter months
winterMonths = [11, 12, 1]; % any order but must be consecutive months
isWinter = ismember(m(:,2), winterMonths);
% group the consecutive winter months
L = bwlabel(isWinter); %requires image processing toolbox
% -------------------------------------------------------
% % If you don't have image processing tool box: %
% isWinterCS = cumsum(diff([0;isWinter]) == 1); %
% L = zeros(size(isWinter)); %
% L(isWinter) = isWinterCS(isWinter); %
% -------------------------------------------------------
% Compute max dischage per annual winter
maxDischarge = grpstats(m(:,4),L,'max'); %requires stats & ML toolbox
% Organize data by year (using the earliest year in each winter)
maxDischargeYear = grpstats(m(:,1),L,'min'); %requires stats & ML toolbox
% -------------------------------------------------------
% % If you don't have stats & ML tool box: %
% groupID = findgroups(L); %
% maxDischarge = splitapply(@max,m(:,4),groupID); %
% maxDischargeYear = splitapply(@min,m(:,1),groupID); %
% -------------------------------------------------------
maxDischargeYear(1) = []; % Remove group 0 (non-winter months)
maxDischarge(1) = []; % Remove group 0 (non-winter months)
T = table(maxDischargeYear(:),maxDischarge(:),'VariableNames',{'Year','MaxDischarge'});
% Show first few rows
head(T)
% Year MaxDischarge
% ____ ____________
%
% 1883 89.476
% 1884 89.657
% 1885 89.815
% 1886 89.866
% 1887 89.884
% 1888 88.552
% 1889 89.299
% 1890 89.571
3 个评论
更多回答(1 个)
Patrick Gipper
2020-1-3
Less elegent and more brute force, but no toolbox required. Not sure what would happen with a missing year or if there happens to be multiple days in a given winter with the same maximum.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!