我目前有兩個時間軸(timeline1和timeline2),以及匹配的數據(data1和data2)。時間表幾乎相同,但不完全相同(約爲常見值的90%)。如何在Matlab中優化這個時間軸匹配代碼?
我試圖找到數據1和數據2對應於相同的時間戳值(忽略所有其他值)
我的第一個簡單的實現如下(和顯然是非常緩慢的,因爲我的時間表包含數千的值)。任何想法如何改善?我敢肯定有這樣做,同時避免for循環,或查找操作的一個聰明的辦法...
% We expect the common timeline to contain
% 0 1 4 5 9
timeline1 = [0 1 4 5 7 8 9 10];
timeline2 = [0 1 2 4 5 6 9];
% Some bogus data
data1 = timeline1*10;
data2 = timeline2*20;
reconstructedData1 = data1;
reconstructedData2 = zeros(size(data1));
currentSearchPosition = 1;
for t = 1:length(timeline1)
% We only look beyond the previous matching location, to speed up find
matchingIndex = find(timeline2(currentSearchPosition:end) == timeline1(t), 1);
if isempty(matchingIndex)
reconstructedData1(t) = nan;
reconstructedData2(t) = nan;
else
reconstructedData2(t) = data2(matchingIndex+currentSearchPosition-1);
currentSearchPosition = currentSearchPosition+matchingIndex;
end
end
% Remove values from data1 for which no match was found in data2
reconstructedData1(isnan(reconstructedData1)) = [];
reconstructedData2(isnan(reconstructedData2)) = [];
啊哈!我知道必須有東西(我只是不知道相交)謝謝! – Kena 2011-01-27 16:39:53