Calculating Discharge Over Multiple Years

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!

 采纳的回答

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 个评论

Thank you Adam this helps alot, however I dont have image processing toolbox how would I do this without using it?
Thanks
I added 3 lines to my solution that you can use if you do not have image processing toolbox. And I added another 3 lines you can use if you don't have Stats & ML Toolbox.
See my updated answer.
This working perfectly aswell thank you for all of your help!

请先登录,再进行评论。

更多回答(1 个)

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.

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by