Clear Filters
Clear Filters

Find max values for each year

5 views (last 30 days)
Danilo M
Danilo M on 21 Dec 2018
Commented: Mahtab Moalemi on 17 Apr 2020
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?

Accepted Answer

Star Strider
Star Strider on 21 Dec 2018
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 Comments
Star Strider
Star Strider on 16 Apr 2020
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 on 17 Apr 2020
I have done that too! My question had the exact same title just the format was different. So it is actually related anyway.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!