Filter löschen
Filter löschen

Find max values for each year

5 Ansichten (letzte 30 Tage)
Danilo M
Danilo M am 21 Dez. 2018
Kommentiert: Mahtab Moalemi am 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?

Akzeptierte Antwort

Star Strider
Star Strider am 21 Dez. 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 Kommentare
Star Strider
Star Strider am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by