我有一个二维矩阵[date x data]。日期格式为Matlab时间,并且跨越30年以上的数据。我想找到6月,7月和8月的数据。如何遍历日期列以查找JJA月的所有每日数据并从中创建新变量?日期栏从1958年至2014年跨越整年(1月至12月)。
样本数据如下:
715290 248.466883960556
715291 283.505916006759
715292 324.290798860324
715293 330.142892431377
715294 267.062371422836
715295 263.568232655174
715296 540.856981589398
715297 1068.81389065867
715298 1174.92651788078
715299 1077.71223624073
715300 940.399428121956
715301 720.323217401065
715302 689.605704068148
715303 777.776178783704
715304 914.330565109213
715305 1069.02532264344
715306 1168.15631281824
715307 1263.47638011252
715308 1309.37995956891
715309 1318.50751550512
715310 1303.83817524424
715311 1273.11884252625
715312 1272.70005316829
715313 1330.08825971279
715314 1391.65459098343
715315 1511.13670010565
715316 1524.18921565080
715317 1451.41725782868
715318 1384.63128177358
715319 1388.90746497726
715320 1423.66479419858
715321 1417.13642861071
我尝试了以下内容,但索引('idx'变量)无法从'data'矩阵中成功获取Matlab日期时间列:
t = datevec(data(:,1)); % get the date value
[unDates, ~, subs] = unique(t(:,1:2),'rows'); % group by unique month
idx = find(unDates(:,2) == 6 | unDates(:,2) == 7 | unDates(:,2) == 8); %Find JJA months
time_JJA = unDates(idx); %unDates is a 2D matrix [YYYY x M]. Col 1 = year and Col 2 = month (e.g., '6' is June)
答案 0 :(得分:1)
我总是喜欢使用matlab日期时间格式,因为我发现它非常方便。然后你可以使用特殊日期操作,它变得非常简单:
%creating datetimes
DataAsDatetime=datetime(data(:,1), 'ConvertFrom', 'datenum');
%getting a vector of month
DataMonth=month(DataAsDatetime);
%creating that logical vector
Logicalvector=(DataMonth==6 | DataMonth==7 | DataMonth==8);
%getting what you want
WhatYouWant=X(Logicalvector,2);
%or all of that in one line
WhatYouWant2=X(month(datetime(data(:,1), 'ConvertFrom', 'datenum'))>=6 & month(datetime(data(:,1), 'ConvertFrom', 'datenum'))<=8,2);
我建议您在导入后立即转换为datetime。在这种情况下,您可以更详细地了解,例如寻找工作日或过滤特定年份