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

时间:2016-07-11 23:27:34

标签: matlab date datetime comparison

I have 2 sets of Date, their 1st and last dates are the same respectively but their dates within might not be the same to each other. Both DateA and DateB contain different values on their each date, which are arrays A and 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]

I have converted the dates into datenumber,ie

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

Now I want to compare the value of A on DateA(n) to that of B on DateB while DateB is the date that is closest to and before the date of DateA(n).

For example,

comparing the value of A on DateA '2016-01-12' to that of B on DateB '2016-01-11'.

Please help and thanks a lot.

1 个答案:

答案 0 :(得分:-1)

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

public event EventHandler<EventArgs> ShowPictureBox;

protected void OnShowPictureBox()
{
    if (ShowPictureBox != null)
        ShowPictureBox(this, EventArgs.Empty);
}

private void Button_Click(object sender, EventArgs e)
{
    OnShowPictureBox();
}

<强>输出: -

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