0

同じ ID を持つ 2 つのマトリックスがあります。mat1の日付から ±5 日以内の日付を持つID の行を抽出する必要がありmat2ます。mat2こちらも同様の操作。こちらのデータをご覧ください: UNIQCols = [1 2] ; dateCol = [3] ; valueCol = [4] ; dayRange = +- 15days.

      % UniqCol  Date    Value
mat1 = [2001 2   733427  1001 ;
        2001 2   733793  2002 ;
        2001 2   734582  2003 ;
        3001 1   734220  30   ;
        3001 1   734588  20   ;];
mat2 = [2001 2   733790  7777 ;
        2001 2   734221  2222 ; 
        3001 1   734220  10   ; 
        3001 1   734588  40   ;] ;

ans1 = [2001 2 733793 2002 ; 3001 1 734220 30 ; 3001 1 734588 20 ] ;
ans2 = [2001 2 733790 7777 ; 3001 1 734220 10 ; 3001 1 734588 40 ] ;

これはベクトル化された操作である必要があります! ID は日付の昇順で並べられます。日付は、Q または年単位で区切られます。したがって、範囲は常に << (date2-date1) になります。助けていただき、ありがとうございます!

4

1 に答える 1

0

これは、コメントで言及した同様の質問に基づく関数です。行列は日付順に並べ替える必要があることに注意してください。

function match_for_xn = match_by_distance(xn, xm, maxdist)
%#Generates index for elements in vector xn that close to any of elements in
%#vector xm at least by distance maxdist

match_for_xn = false(length(xn), 1);
last_M = 1;
for N = 1:length(xn)
  %# search through M until we find a match.
  for M = last_M:length(xm)
    dist_to_curr = xm(M) - xn(N);
    if abs(dist_to_curr) < maxdist
        match_for_xn(N) = 1;
        last_M = M;
        break
    elseif dist_to_curr > 0
        last_M = M;
        break
    else
      continue
    end

  end %# M
end %# N

そしてテストスクリプト:

mat1 = sortrows([
        2001 2   733427  1001 ;
        2001 2   733793  2002 ;
        2001 2   734582  2003 ;
        3001 1   734220  30   ;
        3001 1   734588  20   ;
       ],3);
mat2 = sortrows([
        2001 2   733790  7777 ;
        2001 2   734221  2222 ; 
        3001 1   734220  10   ; 
        3001 1   734588  40   ;
       ],3);

mat1_index = match_by_distance(mat1(:,3),mat2(:,3),5);
ans1 = mat1(mat1_index,:);
mat2_index = match_by_distance(mat2(:,3),mat1(:,3),5);
ans2 = mat2(mat2_index,:);

あなたの問題に対してベクトル化されたソリューションを試していません。このソリューションに対して試してみて、タイミングとメモリ消費量を確認してください (並べ替え手順を含めてください)。

于 2011-03-22T21:15:08.457 に答える