Find max values for each year
조회 수: 11 (최근 30일)
이전 댓글 표시
I have a time series matrix with hourly rain data like this:
[year month day hour minute secont rain]
rain = [2008 1 12 8 0 0
2008 1 12 9 0 0
2008 1 12 10 0 0
2008 1 12 11 0 0
2008 1 12 12 0 0.2
2008 1 12 13 0 0.2
2008 1 12 14 0 1
2008 1 12 15 0 1.6
2008 1 12 16 0 2.2
2008 1 12 17 0 1.6
2008 1 12 18 0 0.8
2008 1 12 19 0 1.6
2008 1 12 20 0 0.8
2008 1 12 21 0 0.6
2008 1 12 22 0 0.6
2008 1 12 23 0 0.8
2008 1 13 0 0 0.2
2008 1 13 1 0 1];
And I need to find the max value of rain(:,7) for each year, resulting in a matrix like
max = [2008 43.2
2009 41.4
2010 30.4
2011 36.6
2012 38.0];
How can I do this on MatLab?
댓글 수: 0
채택된 답변
Star Strider
2018년 12월 21일
Your ‘rain’ array does not exactly match the header information you posted for it, since there are only 6 columns.
This fills in the ‘seconds’ column with a vector of zeros, then does what you requested:
rain = [2008 1 12 8 0 0
2008 1 12 9 0 0
2008 1 12 10 0 0
2008 1 12 11 0 0
2008 1 12 12 0 0.2
2008 1 12 13 0 0.2
2008 1 12 14 0 1
2008 1 12 15 0 1.6
2008 1 12 16 0 2.2
2008 1 12 17 0 1.6
2008 1 12 18 0 0.8
2008 1 12 19 0 1.6
2008 1 12 20 0 0.8
2008 1 12 21 0 0.6
2008 1 12 22 0 0.6
2008 1 12 23 0 0.8
2008 1 13 0 0 0.2
2008 1 13 1 0 1];
Times = datetime([rain(:,1:5) zeros(size(rain,1),1)]); % Create ‘datetime’ Array
TData = timetable(Times, rain(:,6)); % Convert To ‘timetable’
YrMean = retime(TData, 'yearly', 'max'); % Yearly Maximum
Max = [YrMean.Times.Year YrMean.Var1]
producing (with the posted data):
Max =
2008 2.2
Also, don’t name it ‘max’, since that overshadows the MATLAB max function. I capitalised it here to prevent that (MATLAB is case-sensitive).
댓글 수: 3
Star Strider
2020년 4월 16일
Mahtab Moalemi —
Your Comment does not directly relate to this thread.
Please post this as a new Question, then delete your Comment.
Mahtab Moalemi
2020년 4월 17일
I have done that too! My question had the exact same title just the format was different. So it is actually related anyway.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!