data loop for matlab

조회 수: 2 (최근 30일)
zakary Surprenant
zakary Surprenant 2020년 11월 17일
편집: Adam Danz 2020년 12월 8일
for i= 1977:2012
for j=1:12
for k=1:31
mx=min(q1,[],'omitnan');
end
end
end
  댓글 수: 4
Adam Danz
Adam Danz 2020년 11월 18일
How are your data arranged? Could you show us a sample?
zakary Surprenant
zakary Surprenant 2020년 11월 18일

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

채택된 답변

Adam Danz
Adam Danz 2020년 11월 18일
  1. Read data in as a table (see readtable)
  2. Convert the year-month-day columns to a single datetime vector and convert the table to a timetable
  3. Use retime to get the monthly max
Demo:
% Create demo table
T = table([1977;1977;1977;1977;1978;1978],[11;11;12;12;1;1],[1;2;1;2;1;2],(2:7)','VariableNames',{'Year','Month','Day','MaxTemp'})
T = 6x4 table
Year Month Day MaxTemp ____ _____ ___ _______ 1977 11 1 2 1977 11 2 3 1977 12 1 4 1977 12 2 5 1978 1 1 6 1978 1 2 7
% Convert to datetime values
TT = timetable(datetime(T.Year,T.Month,T.Day),T.MaxTemp,'VariableNames',{'MaxTemp'})
TT = 6x1 timetable
Time MaxTemp ___________ _______ 01-Nov-1977 2 02-Nov-1977 3 01-Dec-1977 4 02-Dec-1977 5 01-Jan-1978 6 02-Jan-1978 7
% Get monthly max
TT_MonthlyMax = retime(TT,'Monthly','max')
TT_MonthlyMax = 3x1 timetable
Time MaxTemp ___________ _______ 01-Nov-1977 3 01-Dec-1977 5 01-Jan-1978 7
If any month is missing from the data, the MaxTemp for that month will be NaN. Those rows can be removed with,
TT_MonthlyMax(isnan(TT_MonthlyMax.MaxTemp),:) = [];
  댓글 수: 20
Steven Lord
Steven Lord 2020년 12월 8일
An empty cellstr is by {0x0 char} whereas a missing cellstr could be represented by {''} or perhaps {NaC} (Not-a-char, when the entire char array is 'missing') which would be consistent with cell arrays of doubles (NaN) or datetimes (NaT), etc.
Instead of trying to find some way to represent missing data in a cellstr, I probably would store my text data (including missing) in a string array and use the string array instead of a cellstr.
A = ["apple"; "banana"; "cherry"; missing; "eggplant"]
A = 5×1 string array
"apple" "banana" "cherry" <missing> "eggplant"
Adam Danz
Adam Danz 2020년 12월 8일
편집: Adam Danz 2020년 12월 8일
Thanks, Steven Lord, strings do solve that problem and they distinguish between empties and missing.
A = ["apple"; ""; "cherry"; missing; "eggplant"]
A = 5×1 string array
"apple" "" "cherry" <missing> "eggplant"
But the bigger picture I'd like to emphasize is the option to treat empties as missing in the fillmissing(), rmmissing() etc family of functions. The current workaround is to find the empty arrays and replace them with missing values and for most data types, the missing() function comes in handy to fill with <missing> but missing() does not support cell arrays. So I'm suggesting that the _missing() family of functions optionally treat empty arrays as missing and to do this for empty cells as well.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by