필터 지우기
필터 지우기

Find the indices between two vectors of dates

조회 수: 8 (최근 30일)
Pedro Cavaco
Pedro Cavaco 2011년 4월 5일
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
Fangjun Jiang 2011년 4월 5일
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
Pedro Cavaco
Pedro Cavaco 2011년 4월 5일
This works perfectly. Thanks a lot Fangjun.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by