matlab - 只保留24个值存在的日子

时间:2013-07-11 00:47:11

标签: matlab date accumarray

假设我有一个数据集:

Jday = datenum('2009-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
    datenum('2009-01-05 23:00','yyyy-mm-dd HH:MM');
DateV = datevec(Jday);
DateV(4,:) = [];
DateV(15,:) = [];
DateV(95,:) = [];

Dat = rand(length(Jday),1)

如何删除少于24次测量的所有日子。例如,在第一天只有23次测量,因此我需要移除整整一天,我怎么能为所有阵列重复这个?

2 个答案:

答案 0 :(得分:1)

相当长的答案,但我认为它应该是有用的。我会使用containers.Map来做到这一点。可能有更快的方式,但也许现在这个会很好。

Jday = datenum('2009-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
    datenum('2009-01-05 23:00','yyyy-mm-dd HH:MM');

DateV = datevec(Jday);
DateV(4,:) = [];
DateV(15,:) = [];
DateV(95,:) = [];


% create a map
dateMap = containers.Map();



% count measurements in each date (i.e. first three columns of DateV)
for rowi = 1:1:size(DateV,1)

    dateRow = DateV(rowi, :);
    dateStr = num2str(dateRow(1:3));

    if ~isKey(dateMap, dateStr)
        % initialize Map for a given date with 1 measurement (i.e. our
        % counter of measuremnts
        dateMap(dateStr)  = 1;
        continue;
    end
    % increment measurement counter for given date
    dateMap(dateStr)  = dateMap(dateStr) + 1;
end


% get the dates
dateStrSet = keys(dateMap);




for keyi = 1:numel(dateStrSet)

    dateStrCell = dateStrSet(keyi);  
    dateStr = dateStrCell{1};

    % get number of measurements in a given date
    numOfmeasurements = dateMap(dateStr);

    % if less then 24 do something about it, e.g. save the date
    % for later removal from DateV
    if numOfmeasurements < 24
        fprintf(1, 'This date has less than 24 measurement: %s\n', dateStr);
    end
end

结果是:

This date has less than 24 measurement: 2009     1     1
This date has less than 24 measurement: 2009     1     5

答案 1 :(得分:1)

快速解决方案是按年份,月份,日期对unique()进行分组,然后使用accumarray()计算每天的观察次数,并将逻辑索引的两个步骤排除在少于24个障碍物的情况下

% Count observations per day
[unDate,~,subs] = unique(DateV(:,1:3),'rows');
counts = [unDate accumarray(subs,1)]
counts =
        2009           1           1          22
        2009           1           2          24
        2009           1           3          24
        2009           1           4          24
        2009           1           5          23

然后,将标准应用于计数并检索逻辑索引

% index only those that meet criteria
idxC = counts(:,end) == 24
idxC =
      0
      1
      1
      1
      0

% keep those which meet criteria (optional, for visual inspection)
counts(idxC,:)
ans =
        2009           1           2          24
        2009           1           3          24
        2009           1           4          24

最后,通过Dat找到属于第二轮逻辑indexinf的counts成员{/ 1}}:

ismember()