【问题标题】:How to compare 2 sets of different date which contains 2 different sets of data?如何比较包含 2 组不同数据的 2 组不同日期?
【发布时间】:2016-07-11 23:27:33
【问题描述】:

我有 2 组日期,它们的第一个和最后一个日期分别相同,但它们的日期可能彼此不同。 DateA 和 DateB 的每个日期都包含不同的值,即数组 A 和 B。

DateA=    '2016-01-01'
          '2016-01-02'
          '2016-01-04'
          '2016-01-05'
          '2016-01-06'
          '2016-01-07'
          '2016-01-08'
          '2016-01-09'
          '2016-01-10'
          '2016-01-12'
          '2016-01-13'
          '2016-01-14'
          '2016-01-16'
          '2016-01-17'
          '2016-01-18'
          '2016-01-19'
          '2016-01-20'


DateB=    '2016-01-01'
          '2016-01-02'
          '2016-01-03'
          '2016-01-04'
          '2016-01-05'
          '2016-01-09'
          '2016-01-10'
          '2016-01-11'
          '2016-01-12'
          '2016-01-13'
          '2016-01-15'
          '2016-01-16'
          '2016-01-17'
          '2016-01-19'
          '2016-01-20'

A = [5, 2, 3, 4, 6, 1, 7, 9, 3, 6, 1, 7, 9, 2, 1, 4, 6]

B = [4, 2, 7, 1, 8, 4, 9, 5, 3, 9, 3, 6, 7, 2, 9]

我已将日期转换为日期数字,即

datenumberA=    736330
                736331
                736333
                736334
                736335
                736336
                736337
                736338
                736339
                736341
                736342
                736343
                736345
                736346
                736347


datenumberB=    736330
                736331
                736332
                736333
                736334
                736338
                736339
                736340
                736341
                736342
                736344
                736345
                736346
                736348
                736349

现在我想比较 DateA(n) 上 A 的值与 DateB 上 B 的值,而 DateB 是最接近和早于 DateA(n) 日期的日期。

例如,

比较日期 A '2016-01-12' 上 A 的值与 DateB '2016-01-11' 上 B 的值。

请帮忙,非常感谢。

【问题讨论】:

  • 比较是什么意思?你期待什么结果?
  • 查看A是否大于对应的B,结果应该是1(大于)或0(不大于)
  • 如你所写:“DateB 是最接近和早于DateA(n) 的日期”,当DateA 是'2016-01-01' 时应该是什么结果?由于 DateB 中没有最接近且在 '2016-01-01' 之前的条目?
  • DateA 的第一个顺序不需要比较,因为 DateA 的第一个顺序和 DataB 的第一个顺序相同,因此将是例外的。抱歉,我没有提到。

标签: matlab date datetime comparison


【解决方案1】:

它将为您提供所需的输出!

all_k=0;
out(1)=1;    % not comparing the first index as you mentioned
for n=2:size(datenumberA,1)
    j=0;
    while 1
        k=find(datenumberB+j==datenumberA(n)-1); %finding the index of DateB closest to and before DateA(n)
        if size(k,1)==1 break; end %if found, come out of the while loop
        j=j+1;         % otherwise keep adding 1 in the values of datenumberB until found
    end
    if size(find(all_k==k),2) ~=1 % to avoid if any DateB is already compared
        out(end+1)=A(n)> B(k); %Comparing Value in A with corresponding value in B
        all_k(end+1)=k; end %Storing which indices of DateB are already compared
end
out'   %Output

输出:-

ans =

 1
 0
 0
 1
 0
 0
 1
 0
 0
 1
 0
 0
 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多