如何比较包含两组不同数据的两组不同日期?

How to compare 2 sets of different date which contains 2 different sets of data?

我有两组日期,第一个和最后一个日期分别相同,但它们之间的日期可能不相同。 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) 的日期。

例如,

比较 DateA“2016-01-12”上 A 的值与 DateB“2016-01-11”上 B 的值。

请帮忙,非常感谢。

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

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