Find max values for each year

5 次查看(过去 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?

采纳的回答

Star Strider
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
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
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by