- the two column vectors are sorted
- a specific date in the first vector may exist in the second vector
- more than one date in the first vector may have the same "immediate date before" in the second vector
how to find the immediate date before in one column based on another column using find function?
조회 수: 1 (최근 30일)
이전 댓글 표시
i have 2 columns with double type.
first column is 80 x 1 with double type
second column is 238 X 1 with double type.
we need to find the immediate date before the date present in Column 1. column 1 has date as 20020102 we need to find date which date is immediate before this date . in this case for 20020201(column 1) the immediate date before is 20011231(column 2).
댓글 수: 0
채택된 답변
per isakson
2021년 10월 30일
편집: per isakson
2021년 10월 30일
Assumptions:
Try this example with your data
%%
c1 = [ 20020102; 20020107; 20020115; 20020129 ];
c2 = [ 20020101; 20020103; 20020104; 20020106; 20020115; 20020128; 20020130; 20020131];
%%
tic
c = [ c1+eps(1e9); c2 ];
[b,ixs] = sort( c );
for jj = 1 : numel(c1)
ix = find(ixs==jj)-1;
fprintf( '%9d,%9d\n', round(c1(jj)), b(ix) );
end
toc
%%
tic
for jj = 1 : numel(c1)
ix = find( c2<=c1(jj), 1,'last');
fprintf( '%9d,%9d\n', c1(jj), c2(ix) );
end
toc
%%
tic
ix = interp1( c2, (1:8), c1, 'previous' );
c2(ix)
toc
There is a bug in the first solution.
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!