Calculating Discharge Over Multiple Years
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Adam Danz
on 2 Jan 2020
Edited: Adam Danz
on 3 Jan 2020
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 Comments
More Answers (1)
Patrick Gipper
on 3 Jan 2020
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.
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!