2016-10-18 4 views
1

時間の価値に関してテーブルに加わりたいです。タイムスタンプは、私が供給したい値の間でわずかに異なるので、2つのタイムスタンプの差よりも小さい絶対閾値が同じとみなされる。限られた前払いで外出する方法

Aは私が何を意味するか説明するMWEを追加しました:

になり
t1 = [1476369169.1, 1476369169.2, 1476369169.3, 1476369169.4, 1476369169.5]; 
TableA = table(t1', [1, 2, 3, 4, 5]', 'VariableNames', {'Time', 'A'}); 

t2 = [1476369169.1, 1476369169.3, 1476369169.4, 1476369169.5]; 
PreciseTableB = table(t2', [1, 3, 4, 5]', 'VariableNames', {'Time', 'B'}); 

PreciseJoin = outerjoin(TableA,PreciseTableB, 'Keys', 'Time', 'MergeKeys', 1) 

t4 = t2 + rand(1, 4)/100; 
ErrorTableB = table(t4', [1, 3, 4, 5]', 'VariableNames', {'Time', 'B'}); 
ErrorJoin = outerjoin(TableA,ErrorTableB, 'Keys', 'Time', 'MergeKeys', 1) 

PreciseJoin = 

     Time  A  B 
    ____________ _ ___ 

    1476369169.1 1  1 
    1476369169.2 2 NaN 
    1476369169.3 3  3 
    1476369169.4 4  4 
    1476369169.5 5  5 


ErrorJoin = 

      Time   A  B 
    ________________ ___ ___ 

     1476369169.1  1 NaN 
    1476369169.1095 NaN  1 
     1476369169.2  2 NaN 
     1476369169.3  3 NaN 
    1476369169.30034 NaN  3 
     1476369169.4  4 NaN 
    1476369169.40439 NaN  4 
     1476369169.5  5 NaN 
    1476369169.50382 NaN  5 

今、私は2番目のテーブルは小さいがあるにもかかわらず、最初のように見えることを希望「時間」列内の違い。これは可能ですか?

+0

[diff'](https://www.mathworks.com/help/matlab/ref/diff.html)タイムスタンプを適用し、しきい値を下回る行を削除します。 – excaza

+0

@excaza。これは、値をソートすると機能します。しかし、問題は、間に値がないことです。より直接的なアプローチはありますか? –

+0

なぜ時系列データをソートしないのですか?値をマージする場合は、削除する前に欠落しているデータをチェックします。 – excaza

答えて

1

R2016bをお持ちの場合は、これは新しいtimetableの方法synchronizeの理想的な作業です。中

tbase = seconds([1476369169.1, 1476369169.2, 1476369169.3, 1476369169.4, 1476369169.5]); 
t1 = tbase + seconds(rand(size(tbase))/100); 
t2 = tbase + seconds(rand(size(tbase))/100); 

TimetableA = timetable((1:5)', 'VariableNames', {'A'}, 'RowTimes', t1); 
TimetableB = timetable((1:5)', 'VariableNames', {'B'}, 'RowTimes', t2); 

combined = synchronize(TimetableA, TimetableB, tbase, 'nearest') 

結果:

 
>> combined 
combined = 
      Time   A B 
    ________________ _ _ 
    1476369169.1 sec 1 1 
    1476369169.2 sec 2 2 
    1476369169.3 sec 3 3 
    1476369169.4 sec 4 4 
    1476369169.5 sec 5 5 

あはっは、コメントを次のように、私は私が "欠損値" の問題を逃した実現。これは、おそらくismembertolを使用してR2015a互換の解決策が推奨されることを意味します。

% Use a somewhat extended "base" time-scale 
tbase = 1476369169 + (0:0.1:1)'; 

% Add noise to t1 and t2, selecting different fundamental 
% elements from 'tbase' 
t1 = tbase(1:7) + (rand(size(tbase(1:7)))/100); 
t2 = tbase(2:2:end) + (rand(size(tbase(2:2:end)))/100); 

% Work out which elements of t1 and t2 are members of tbase, within 
% tolerance of 0.01. Use DataScale == 1 for absolute tolerance. 
% In each case, the '_lia' output tells us whether the time 
% vector is present in 'tbase'; and '_locB' tells us where 
% in 'tbase' each element exists (or 0 if the corresponding element 
% of '_lia' is false). 
[t1_lia, t1_locB] = ismembertol(t1, tbase, 0.01, 'DataScale', 1); 
[t2_lia, t2_locB] = ismembertol(t2, tbase, 0.01, 'DataScale', 1); 

% Build tables that we can join together. 
TA = table((1:numel(t1))', t1_locB, t1, 'VariableNames', {'A', 'locB', 'time'}) 
TB = table((1:numel(t2))', t2_locB, t2, 'VariableNames', {'B', 'locB', 'time'}) 

% Filter TA and TB to contain only rows which match 'tbase' 
TA = TA(t1_lia, :); 
TB = TB(t2_lia, :); 

% Join these by location in the common time-base 
TAB = outerjoin(TA, TB, 'Keys', {'locB'}, 'MergeKeys', true); 
TAB.time = tbase(TAB.locB); 
% Don't need the 'locB' variable in this table 
TAB.locB = []; 
TAB 

私のためTABための次の出力を生成します:私はここにAとBの実際の時間を保持してきた

 
TAB = 
    A   time_TA   B   time_TB    time  
    ___ ________________ ___ ________________ ____________ 
     1 1476369169.0NaN     NaN  1476369169 
     2 1476369169.10184  1 1476369169.10491 1476369169.1 
     3  1476369169.2024 NaN     NaN 1476369169.2 
     4 1476369169.30417  2 1476369169.30489 1476369169.3 
     5  1476369169.4005 NaN     NaN 1476369169.4 
     6 1476369169.50903  3 1476369169.50338 1476369169.5 
     7 1476369169.60945 NaN     NaN 1476369169.6 
    NaN     NaN  4  1476369169.709 1476369169.7 
    NaN     NaN  5 1476369169.90369 1476369169.9 

注ここでは、もともと提起として問題へのわずかな拡張があります。

+0

これはそうだね。残念ながら私は2015aです。このソリューションは欠損値も処理できますか? –

+0

別の試みを追加しました。これはR2015aでもうまくいきます。 – Edric

+0

パーフェクト。ちょうど私が探していたもの。 2015年版もありがとう。 –

関連する問題