How do I get a function in a for loop so it applies it to an entire table?

조회 수: 1 (최근 30일)
My code:
t = timetable2table(tt);
t2=t(:,1:2); % here it takes only one station out of a table
t2.Year= dateshift(t.Date,'start','year'); % Here I create a seperate date column, which takes out the time from the date column
rowfun(@myFun,t2,'GroupingVariable','Year','OutputVariableNames',{'Max' 'MaxX'}) % Here it finds the maximum of each year (of one station) and the time the maximum occurred
function [tmax,xmax] = myFun(t,x)
[xmax,imax] = max(x,[],1);
tmax = t(imax);
end
How do I get this to loop for each station so it finds the maximum for each year for each station and the time the maximum occurred?
  댓글 수: 5
Cris LaPierre
Cris LaPierre 2021년 3월 12일
편집: Cris LaPierre 2021년 3월 12일
You don't want rowfun. Rowfun assumes each column of your table is an input to your function, and is applied to each row separately. You kind of want the exact opposite, meaning you want it to look at all rows, and apply to each column separately.
While groupsummary can help you find the yearly max for each station without a loop, to get the corresponding timestamp for each station likely will.
Since it's much easier to test with actual data, consider sharing your table. Save it to a mat file and attach it to your post using the paperclip icon.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 3월 12일
편집: Cris LaPierre 2021년 3월 12일
To follow up, the easiest way to find yearly max is to use groupsummary with the appropriate settings.
load matlabworkspace28.02.mat
yrMax = groupsummary(tt,"Kuupev","year","max",'IncludeMissingGroups',false)
yrMax = 9×44 table
year_Kuupev GroupCount max_AJHAAP01 max_AJHARK01 max_AJHELT01 max_AJJOGE01 max_AJJOHV01 max_AJKIHN01 max_AJKUND01 max_AJKUUS01 max_AJNARV01 max_AJNIGU01 max_AJPAKR01 max_AJPARN01 max_AJRIST01 max_AJROOM01 max_AJRUHN01 max_AJSORV01 max_AJTART01 max_AJTIIR01 max_AJTOOM01 max_AJTURI01 max_AJG01 max_AJVILJ01 max_AJVILS01 max_AJVIRT01 max_AJVMA01 max_AJVORU01 max_HPKOREL max_HPPAJUP max_HPPAJUS max_HPPIIGA max_HPRANNU max_HPRIISA max_HPSAMI max_HPTOLLI max_HPTORVA max_HPVANAK max_HPVIHTE max_MPKOODU max_MPMASSU max_MPOTEPA max_MPTUDU max_MPTUULE ___________ __________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ _________ ____________ ____________ ____________ ___________ ____________ ___________ ___________ ___________ ___________ ___________ ___________ __________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ __________ ___________ 2011 8760 11.3 15.8 20.9 23.9 22.8 21.5 30.4 NaN NaN 13.3 14.7 23.9 9.7 8.8 17.5 19.5 23 25.1 21.2 23.8 21.8 7.9 13.5 12.6 13.6 29.7 NaN NaN 11 10.1 NaN NaN NaN 19.7 NaN NaN 2.5 4.7 2.5 18.5 2.5 1.7 2012 8784 31.3 13.7 14.6 15.4 14.3 10.6 10.3 NaN NaN 19.3 13.7 15.8 13.2 16.3 13.5 23.8 13.6 17.8 11.6 12.9 11.8 25.4 10.1 13.9 30.4 9.2 NaN NaN 15.5 10.4 NaN NaN 1 8.4 NaN 1.5 29.6 19.8 10.4 18.5 12.8 23.6 2013 8760 44.8 18.5 15 15.7 27.4 10.1 12.6 NaN NaN 15.7 14.5 18.9 13.8 21.1 21.2 32.1 14.6 19.6 20.2 11.2 22.9 14 9.8 26.7 5.8 13.2 NaN NaN 12.2 12.9 NaN NaN 15.3 8.1 NaN 14 15.3 9 13.2 12.6 15.4 15.9 2014 8760 16.6 12 16.2 20.2 9.1 12.9 24.6 NaN 9.5 12.5 36.6 18.4 13.5 9.2 21.4 12.9 14.8 12.8 19.4 11.2 23.4 14.2 12 13.8 22.8 8.1 NaN NaN 19.9 9.1 NaN NaN 32.1 11.3 NaN 12.4 14.7 19.6 17 28.1 8.8 27.7 2015 8760 9.2 10 6.9 7.2 6.1 18.6 15.1 NaN 9.9 12 13.7 5.5 7.2 8.4 13.3 9.8 9.5 11.6 7.9 11.4 14.4 17.5 5.7 10.5 8.7 20 NaN NaN 11.4 13.9 NaN NaN 12.9 7.6 NaN 9.9 18.5 13.2 12.6 17 8.5 9.2 2016 8784 11.6 21 13.8 19.3 43.1 38.7 19.8 NaN 35.6 14.7 10.5 8.1 9.4 9.8 8.6 10.5 30 20.9 20.2 21.8 25.7 12.7 14 12.2 12.2 28.5 NaN NaN 23.2 22.4 NaN NaN 15.1 14.3 NaN 28.6 22.6 11 13.6 20.9 11.6 10.5 2017 8760 10.3 23.8 12.2 20.6 16.8 21.6 21.9 NaN 12.5 8.7 24.3 6.9 6.1 6.5 11 10 22 12.7 18.5 13.8 18.2 18.8 6.1 7.9 8.8 14.5 NaN NaN 20.3 9.7 NaN NaN 11.7 31.1 NaN 24.3 16.7 10.5 14.1 19.2 15.5 16.8 2018 8760 16 15.7 6.9 5.2 10.4 8.9 5.1 13 8.3 8.5 6.9 15.3 14.3 6.5 10 9.7 8.9 28.3 8.6 22.3 9.1 10.8 7.4 16.5 7.1 12.2 NaN NaN 19.4 12.5 NaN NaN 15.4 10.7 NaN 5.5 9.9 50.6 8.2 7.9 18.8 9.9 2019 8456 14 13.1 22.9 16.8 24 19 8.5 17.3 8.8 10.5 10.3 22.3 14.1 18.6 21.9 13.4 18.4 19 22 27 18.6 20.9 16 9.3 14.1 24.3 9.4 12.6 14.9 11.6 12.5 12.7 10 21.9 19.3 10.4 8 19.9 23.6 20.6 10.6 NaN
However, this will not give you the corresponding timestamp for each max. There is no slick way I could think of to do that, so here's my brute force attempt.
% Find the years in the timetable
Y = min(year(tt.Kuupev)):max(year(tt.Kuupev));
% create a new table to hold the results
yrTbl = array2table(NaT(length(Y),2*width(tt)),"VariableNames",["Date" "Station"]' + (1:width(tt)));
yrTbl = convertvars(yrTbl,2:2:width(yrTbl),@datenum);
% loop through each year
for y = 1:length(Y)
% find the max value for each station plus the corresponding index
dateInd = find(year(tt.Kuupev)==Y(y));
[val,ind] = max(tt{dateInd,:});
% Assign values to output table
C = 1:2:width(yrTbl);
yrTbl{y,C} = tt.Kuupev(dateInd(ind))';
yrTbl{y,C+1} = val;
% If max is NaN, set time to NaT
yrTbl{y,C(isnan(val))} = NaT;
end
% View results
yrTbl
yrTbl = 9×84 table
Date1 Station1 Date2 Station2 Date3 Station3 Date4 Station4 Date5 Station5 Date6 Station6 Date7 Station7 Date8 Station8 Date9 Station9 Date10 Station10 Date11 Station11 Date12 Station12 Date13 Station13 Date14 Station14 Date15 Station15 Date16 Station16 Date17 Station17 Date18 Station18 Date19 Station19 Date20 Station20 Date21 Station21 Date22 Station22 Date23 Station23 Date24 Station24 Date25 Station25 Date26 Station26 Date27 Station27 Date28 Station28 Date29 Station29 Date30 Station30 Date31 Station31 Date32 Station32 Date33 Station33 Date34 Station34 Date35 Station35 Date36 Station36 Date37 Station37 Date38 Station38 Date39 Station39 Date40 Station40 Date41 Station41 Date42 Station42 ____________________ ________ ____________________ ________ ____________________ ________ ____________________ ________ ____________________ ________ ____________________ ________ ____________________ ________ ____________________ ________ ____________________ ________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ ____________________ _________ 29-Jul-2011 14:00:00 11.3 23-May-2011 07:00:00 15.8 14-Jun-2011 07:00:00 20.9 16-Aug-2011 16:00:00 23.9 23-Jul-2011 11:00:00 22.8 28-Jul-2011 13:00:00 21.5 29-Jul-2011 18:00:00 30.4 NaT NaN NaT NaN 07-Jul-2011 14:00:00 13.3 30-Jul-2011 00:00:00 14.7 30-Aug-2011 00:00:00 23.9 30-Jul-2011 07:00:00 9.7 07-Aug-2011 18:00:00 8.8 11-Jul-2011 21:00:00 17.5 26-Jul-2011 14:00:00 19.5 09-Jul-2011 12:00:00 23 31-Jul-2011 03:00:00 25.1 16-Aug-2011 16:00:00 21.2 28-Jul-2011 12:00:00 23.8 28-Jul-2011 09:00:00 21.8 27-Jul-2011 23:00:00 7.9 29-Jul-2011 15:00:00 13.5 27-Jul-2011 19:00:00 12.6 26-Jul-2011 16:00:00 13.6 28-Jul-2011 20:00:00 29.7 NaT NaN NaT NaN 09-Aug-2011 03:00:00 11 19-Jul-2011 17:00:00 10.1 NaT NaN NaT NaN NaT NaN 22-Aug-2011 18:00:00 19.7 NaT NaN NaT NaN 30-Dec-2011 05:00:00 2.5 15-Dec-2011 06:00:00 4.7 19-Dec-2011 23:00:00 2.5 12-Jul-2011 15:00:00 18.5 27-Dec-2011 11:00:00 2.5 14-Dec-2011 14:00:00 1.7 07-Jul-2012 13:00:00 31.3 16-Jul-2012 00:00:00 13.7 07-Jul-2012 05:00:00 14.6 19-Aug-2012 16:00:00 15.4 26-Jun-2012 14:00:00 14.3 23-Aug-2012 10:00:00 10.6 30-Jul-2012 18:00:00 10.3 NaT NaN NaT NaN 27-Sep-2012 19:00:00 19.3 07-Jul-2012 15:00:00 13.7 07-Jul-2012 07:00:00 15.8 23-Aug-2012 17:00:00 13.2 06-Aug-2012 21:00:00 16.3 07-Jul-2012 14:00:00 13.5 07-Jul-2012 04:00:00 23.8 27-Aug-2012 13:00:00 13.6 12-Jul-2012 18:00:00 17.8 26-Jun-2012 12:00:00 11.6 12-Jul-2012 13:00:00 12.9 12-Jul-2012 20:00:00 11.8 30-Jul-2012 15:00:00 25.4 07-Aug-2012 14:00:00 10.1 27-Sep-2012 19:00:00 13.9 17-May-2012 13:00:00 30.4 15-Jul-2012 04:00:00 9.2 NaT NaN NaT NaN 19-Aug-2012 12:00:00 15.5 30-Jul-2012 17:00:00 10.4 NaT NaN NaT NaN 07-Dec-2012 15:00:00 1 27-Aug-2012 13:00:00 8.4 NaT NaN 25-Dec-2012 12:00:00 1.5 07-Jul-2012 16:00:00 29.6 08-Aug-2012 08:00:00 19.8 30-Jul-2012 15:00:00 10.4 12-Jul-2012 15:00:00 18.5 06-Aug-2012 22:00:00 12.8 17-Aug-2012 12:00:00 23.6 17-May-2013 17:00:00 44.8 09-Aug-2013 00:00:00 18.5 22-May-2013 03:00:00 15 18-May-2013 18:00:00 15.7 19-May-2013 18:00:00 27.4 30-Jul-2013 21:00:00 10.1 08-Aug-2013 00:00:00 12.6 NaT NaN NaT NaN 08-Aug-2013 22:00:00 15.7 08-Aug-2013 22:00:00 14.5 11-Aug-2013 20:00:00 18.9 29-May-2013 18:00:00 13.8 09-Aug-2013 01:00:00 21.1 18-May-2013 17:00:00 21.2 18-May-2013 08:00:00 32.1 18-May-2013 14:00:00 14.6 18-Jul-2013 18:00:00 19.6 18-Jul-2013 17:00:00 20.2 15-Aug-2013 05:00:00 11.2 25-Jun-2013 12:00:00 22.9 22-Jul-2013 22:00:00 14 08-Aug-2013 20:00:00 9.8 18-May-2013 18:00:00 26.7 04-Nov-2013 09:00:00 5.8 19-Sep-2013 02:00:00 13.2 NaT NaN NaT NaN 05-Jul-2013 13:00:00 12.2 26-Jun-2013 11:00:00 12.9 NaT NaN NaT NaN 29-Jun-2013 19:00:00 15.3 06-Jun-2013 15:00:00 8.1 NaT NaN 29-Jun-2013 06:00:00 14 08-Aug-2013 22:00:00 15.3 01-Jun-2013 03:00:00 9 10-Jun-2013 17:00:00 13.2 20-May-2013 19:00:00 12.6 01-Aug-2013 19:00:00 15.4 14-Jul-2013 19:00:00 15.9 13-Jul-2014 18:00:00 16.6 29-Jul-2014 11:00:00 12 16-Aug-2014 14:00:00 16.2 23-Aug-2014 14:00:00 20.2 25-May-2014 17:00:00 9.1 11-Aug-2014 22:00:00 12.9 21-May-2014 14:00:00 24.6 NaT NaN 06-Jun-2014 15:00:00 9.5 13-Jul-2014 19:00:00 12.5 12-Jun-2014 11:00:00 36.6 16-Aug-2014 07:00:00 18.4 25-Sep-2014 03:00:00 13.5 19-May-2014 07:00:00 9.2 31-Jul-2014 10:00:00 21.4 27-Aug-2014 00:00:00 12.9 20-Jul-2014 07:00:00 14.8 21-May-2014 15:00:00 12.8 12-Jun-2014 20:00:00 19.4 19-Aug-2014 12:00:00 11.2 06-Jun-2014 19:00:00 23.4 06-Jul-2014 16:00:00 14.2 06-Aug-2014 19:00:00 12 31-Jul-2014 15:00:00 13.8 06-Jun-2014 12:00:00 22.8 20-Jul-2014 08:00:00 8.1 NaT NaN NaT NaN 06-Jul-2014 18:00:00 19.9 19-Jul-2014 15:00:00 9.1 NaT NaN NaT NaN 20-Jul-2014 00:00:00 32.1 28-Jul-2014 20:00:00 11.3 NaT NaN 12-Aug-2014 01:00:00 12.4 12-Jun-2014 11:00:00 14.7 24-Jul-2014 16:00:00 19.6 31-Jul-2014 13:00:00 17 29-Jul-2014 16:00:00 28.1 10-Sep-2014 09:00:00 8.8 20-Jul-2014 08:00:00 27.7 06-Jul-2015 20:00:00 9.2 17-Jul-2015 09:00:00 10 01-Aug-2015 07:00:00 6.9 05-Sep-2015 07:00:00 7.2 28-Sep-2015 06:00:00 6.1 06-Jul-2015 18:00:00 18.6 17-Jul-2015 12:00:00 15.1 NaT NaN 25-Sep-2015 20:00:00 9.9 06-Jul-2015 21:00:00 12 31-Jul-2015 03:00:00 13.7 30-Jul-2015 08:00:00 5.5 06-Jul-2015 16:00:00 7.2 21-Sep-2015 10:00:00 8.4 29-Jul-2015 04:00:00 13.3 11-Jul-2015 00:00:00 9.8 08-Jun-2015 18:00:00 9.5 01-Aug-2015 12:00:00 11.6 08-Jun-2015 15:00:00 7.9 01-Aug-2015 11:00:00 11.4 25-Sep-2015 16:00:00 14.4 06-Aug-2015 02:00:00 17.5 04-Sep-2015 19:00:00 5.7 30-Aug-2015 11:00:00 10.5 07-Sep-2015 20:00:00 8.7 12-Aug-2015 19:00:00 20 NaT NaN NaT NaN 06-Sep-2015 17:00:00 11.4 09-Aug-2015 00:00:00 13.9 NaT NaN NaT NaN 31-Jul-2015 13:00:00 12.9 17-Jul-2015 14:00:00 7.6 NaT NaN 21-Sep-2015 16:00:00 9.9 31-Jul-2015 01:00:00 18.5 21-Sep-2015 16:00:00 13.2 29-Jul-2015 11:00:00 12.6 12-Aug-2015 18:00:00 17 17-Jul-2015 13:00:00 8.5 06-Jul-2015 20:00:00 9.2 29-Sep-2016 20:00:00 11.6 29-Aug-2016 23:00:00 21 15-Jul-2016 06:00:00 13.8 16-Jun-2016 11:00:00 19.3 27-Jun-2016 16:00:00 43.1 29-Aug-2016 05:00:00 38.7 28-Jul-2016 16:00:00 19.8 NaT NaN 03-Jul-2016 13:00:00 35.6 29-Aug-2016 17:00:00 14.7 17-Jun-2016 20:00:00 10.5 31-Jul-2016 11:00:00 8.1 15-Aug-2016 06:00:00 9.4 02-Jul-2016 12:00:00 9.8 06-Aug-2016 15:00:00 8.6 02-Jul-2016 13:00:00 10.5 21-Jun-2016 17:00:00 30 28-Jul-2016 15:00:00 20.9 16-Jun-2016 11:00:00 20.2 02-Aug-2016 09:00:00 21.8 03-Jul-2016 10:00:00 25.7 06-Aug-2016 00:00:00 12.7 26-Jun-2016 07:00:00 14 03-Sep-2016 18:00:00 12.2 16-Jun-2016 12:00:00 12.2 27-Jun-2016 02:00:00 28.5 NaT NaN NaT NaN 03-Jul-2016 11:00:00 23.2 05-Aug-2016 22:00:00 22.4 NaT NaN NaT NaN 03-Jul-2016 12:00:00 15.1 03-Jul-2016 10:00:00 14.3 NaT NaN 10-Jul-2016 10:00:00 28.6 15-Jul-2016 08:00:00 22.6 29-Aug-2016 06:00:00 11 06-Aug-2016 01:00:00 13.6 03-Jul-2016 10:00:00 20.9 16-Aug-2016 08:00:00 11.6 29-Aug-2016 17:00:00 10.5 22-Aug-2017 10:00:00 10.3 17-Aug-2017 16:00:00 23.8 12-Aug-2017 16:00:00 12.2 13-Jul-2017 15:00:00 20.6 04-Jul-2017 13:00:00 16.8 12-Sep-2017 02:00:00 21.6 31-Jul-2017 17:00:00 21.9 NaT NaN 10-Sep-2017 21:00:00 12.5 07-Aug-2017 08:00:00 8.7 06-Aug-2017 02:00:00 24.3 13-Aug-2017 15:00:00 6.9 05-Jun-2017 02:00:00 6.1 18-Nov-2017 15:00:00 6.5 29-Jun-2017 23:00:00 11 25-Aug-2017 16:00:00 10 12-Aug-2017 19:00:00 22 17-Aug-2017 16:00:00 12.7 10-Sep-2017 17:00:00 18.5 19-Jun-2017 12:00:00 13.8 13-Jul-2017 13:00:00 18.2 04-Jul-2017 12:00:00 18.8 25-Oct-2017 18:00:00 6.1 20-Sep-2017 04:00:00 7.9 17-Jul-2017 15:00:00 8.8 04-Aug-2017 05:00:00 14.5 NaT NaN NaT NaN 13-Jul-2017 14:00:00 20.3 11-Jun-2017 15:00:00 9.7 NaT NaN NaT NaN 19-Aug-2017 14:00:00 11.7 18-Jul-2017 09:00:00 31.1 NaT NaN 12-Aug-2017 18:00:00 24.3 05-Aug-2017 11:00:00 16.7 22-Aug-2017 11:00:00 10.5 13-Jul-2017 13:00:00 14.1 11-Jun-2017 15:00:00 19.2 10-Sep-2017 18:00:00 15.5 19-Aug-2017 17:00:00 16.8 24-Aug-2018 20:00:00 16 21-Jun-2018 21:00:00 15.7 02-Oct-2018 10:00:00 6.9 11-Sep-2018 14:00:00 5.2 13-Aug-2018 00:00:00 10.4 11-Aug-2018 22:00:00 8.9 03-Oct-2018 20:00:00 5.1 12-Aug-2018 10:00:00 13 11-Sep-2018 21:00:00 8.3 21-Jun-2018 20:00:00 8.5 20-Aug-2018 14:00:00 6.9 13-Jul-2018 16:00:00 15.3 12-Aug-2018 08:00:00 14.3 27-Oct-2018 06:00:00 6.5 19-Aug-2018 23:00:00 10 24-Aug-2018 18:00:00 9.7 20-Aug-2018 18:00:00 8.9 31-Jul-2018 02:00:00 28.3 30-Aug-2018 13:00:00 8.6 24-Jul-2018 08:00:00 22.3 12-Jun-2018 05:00:00 9.1 24-Jul-2018 10:00:00 10.8 20-Aug-2018 15:00:00 7.4 04-Aug-2018 12:00:00 16.5 31-Jul-2018 15:00:00 7.1 03-Aug-2018 15:00:00 12.2 NaT NaN NaT NaN 04-Aug-2018 11:00:00 19.4 21-Jun-2018 14:00:00 12.5 NaT NaN NaT NaN 31-Jul-2018 01:00:00 15.4 12-Aug-2018 11:00:00 10.7 NaT NaN 15-Sep-2018 17:00:00 5.5 24-Aug-2018 21:00:00 9.9 20-Jul-2018 14:00:00 50.6 12-Jun-2018 06:00:00 8.2 20-Aug-2018 19:00:00 7.9 31-Jul-2018 00:00:00 18.8 05-Aug-2018 12:00:00 9.9 13-Jun-2019 02:00:00 14 13-Jun-2019 19:00:00 13.1 13-Jun-2019 02:00:00 22.9 13-Jun-2019 21:00:00 16.8 03-Jul-2019 03:00:00 24 13-Jun-2019 19:00:00 19 17-Sep-2019 22:00:00 8.5 13-Jun-2019 03:00:00 17.3 05-Jul-2019 11:00:00 8.8 13-Jun-2019 03:00:00 10.5 14-Aug-2019 05:00:00 10.3 13-Jun-2019 19:00:00 22.3 09-Sep-2019 21:00:00 14.1 22-Jul-2019 16:00:00 18.6 11-Jun-2019 19:00:00 21.9 08-Jul-2019 14:00:00 13.4 07-Aug-2019 12:00:00 18.4 13-Jun-2019 07:00:00 19 13-Jun-2019 06:00:00 22 13-Jun-2019 04:00:00 27 13-Jun-2019 20:00:00 18.6 13-Jun-2019 16:00:00 20.9 07-Aug-2019 08:00:00 16 16-Jul-2019 05:00:00 9.3 13-Jun-2019 05:00:00 14.1 12-Aug-2019 18:00:00 24.3 11-Jun-2019 21:00:00 9.4 11-Aug-2019 16:00:00 12.6 09-Aug-2019 11:00:00 14.9 13-Jun-2019 06:00:00 11.6 11-Jun-2019 19:00:00 12.5 08-Jul-2019 10:00:00 12.7 02-Jul-2019 11:00:00 10 24-Jul-2019 16:00:00 21.9 13-Jun-2019 20:00:00 19.3 07-Aug-2019 07:00:00 10.4 23-Aug-2019 19:00:00 8 13-Jun-2019 03:00:00 19.9 13-Jun-2019 21:00:00 23.6 08-Jul-2019 11:00:00 20.6 11-Jun-2019 22:00:00 10.6 NaT NaN
  댓글 수: 2
SaaraL
SaaraL 2021년 3월 12일
Thank you so much! I've been struggling with this for weeks.
I got NaT Nan answer for the last station in year 2019, but there is data. What may be the reason for that?
Cris LaPierre
Cris LaPierre 2021년 3월 12일
You should inspect the data for that year at that station. You might find the answer makes sense.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Prudhvi Peddagoni
Prudhvi Peddagoni 2021년 3월 12일
편집: Prudhvi Peddagoni 2021년 3월 12일
Hi,
First, run a for loop over all the rows of data to take note of the indices of each year. For more info about table indexing, refer this
indices = zeros(1,9); % stores starting indices of years
currentYear = 2011;
for idx = 1:length(t)
if year(t.Date(idx)) == currentYear
currentYear = currentYear+1;
indices(currentYear-2011) = idx;
end
end
After this, use a for loop to go over each year. For each year, extract the rows of that year from the table to a matrix. Then give that matrix to the max function like this:
[maxValues, correspondingIndices] = max(matrix,[],2);
After getting the indices, you can get the corresponding date from the table.
Hope this helps.

Allen
Allen 2021년 3월 12일
편집: Allen 2021년 3월 12일
A relatively general approach to extract max values by year for each table variable (stations in this case) assuming the first variable of the table is a datetime value.
t = timetable2table(tt);
Vars = t.Properties.VariableNames; % Extract Variable Names
Yrs = unique(year(t.(Vars{1}))); % Get unique year values, using t.(Vars{1}) instead of t.Date since data provided had non-English labels.
Yrs(isnan(Yrs)) = []; % Remove NaN values
% Loop for each year and find max value for all stations
xmax = zeros([length(Yrs),length(Vars)-1]); % Pre-allocate for speed
for i=1:length(Yrs)
xmax(i,:) = max(t{year(t.(Vars{1}))==Y(i),2:end});
end
% Convert results to table
tmax = array2table([Yrs,xmax],'VariableNames',Vars);
  댓글 수: 1
SaaraL
SaaraL 2021년 3월 12일
Thanks a lot!
But I also need the corresponding time when each rainfall occurred not just the year in one table. Would you know how to add that too?

댓글을 달려면 로그인하십시오.


Seth Furman
Seth Furman 2021년 3월 22일
To add to Chris' answer, you can also find the yearly max for each station using retime. For example, using the first two columns of the timetable you provided
>> load matlabworkspace28.02.mat
>> ttMax = retime(tt(:,1:2),'yearly','max')
ttMax =
9×2 timetable
Kuupev AJHAAP01 AJHARK01
___________ ________ ________
01-Jan-2011 11.3 15.8
01-Jan-2012 31.3 13.7
01-Jan-2013 44.8 18.5
01-Jan-2014 16.6 12
01-Jan-2015 9.2 10
01-Jan-2016 11.6 21
01-Jan-2017 10.3 23.8
01-Jan-2018 16 15.7
01-Jan-2019 14 13.1

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by