Calculating Discharge Over Multiple Years
조회 수: 2 (최근 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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!