# Find the indices between two vectors of dates

18 views (last 30 days)
Pedro Cavaco on 5 Apr 2011
Hi all,
I have two vectors of dates but they start at different periods and have different hour distributions.
The first vector is inside Matrix A and is even spaced by hour, this is:
A(1,1) = '03-Jan-1983 0:00:00';
A(2,1) = '03-Jan-1983 1:00:00';
A(3,1) = '03-Jan-1983 2:00:00';
A(4,1) = '03-Jan-1983 3:00:00';
...
A(10,1) = '03-Jan-1983 9:00:00';
...
A(end,1) = '01-Jan-2006 23:00:00';
The second vector is inside matrix B but is not even spaced in time.
B(1,1) = '19-Nov-1982 19:00:00';
B(2,1) = '19-Nov-1982 22:00:00';
B(3,1) = '20-Nov-1982 01:00:00';
B(4,1) = '20-Nov-1982 04:00:00';
...
B(end,1) = '31-Dec-2010 23:50:00';
In both matrices the date is just a reference to a value value in column 2.
What i want to do is to create a matrix C which will contain the evenly spaced dates from vector A(:,1) in the first column. The value with respect to that that date from vector A(:,2) and in the third column the value from the vector B(:,2).
C = zeros(length(A(:,1),3)
C = [A(:,1) A(:,2) B(indices,2)]
I want to find only the indices of the dates that match perfectly between the two vector of dates, if no match is found leaves a zero. After I just interpolate between these zeros to find the missing values.
I tried this script to find the indicies:
ind = zeros(length(A(:,1)),1);
for i=1:length(A)
ind(i) = find(datenum(B(:,1)) == datenum(A(i,1)));
end
But it returned this error:
??? Improper assignment with rectangular empty matrix.
Can anybody help????

Fangjun Jiang on 5 Apr 2011
The following code works.
A(1,1) = datenum('03-Jan-1983 0:00:00');
A(2,1) = datenum('03-Jan-1983 1:00:00');
A(3,1) = datenum('03-Jan-1983 2:00:00');
A(4,1) = datenum('03-Jan-1983 3:00:00');
A(:,2)=[1:4]';
B(1,1) = datenum('03-Jan-1983 1:00:00');
B(2,1) = datenum('03-Jan-1983 3:00:00');
B(3,1) = datenum('03-Jan-1983 5:00:00');
B(4,1) = datenum('03-Jan-1983 7:00:00');
B(:,2)=[10:13]';
C = zeros(length(A(:,1)),3);
C(:,1:2)=A;
[TF,LOC]=ismember(A(:,1),B(:,1));
indices=LOC(TF);
C(TF,3)=B(indices,2);
C(:,2:3)
##### 1 CommentShowHide None
Pedro Cavaco on 5 Apr 2011
This works perfectly. Thanks a lot Fangjun.