【发布时间】:2011-01-27 16:31:31
【问题描述】:
我目前有两个时间线(时间线 1 和时间线 2),具有匹配的数据(数据 1 和数据 2)。时间线几乎匹配,但不完全匹配(大约 90% 的常见值)。
我正在尝试从 data1 和 data2 中找到与相同时间戳相对应的值(忽略所有其他值)
我的第一个简单的实现如下(显然非常慢,因为我的时间线包含数千个值)。关于如何改进这一点的任何想法?我确信有一种聪明的方法可以做到这一点,同时避免 for 循环或 find 操作......
% 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)) = [];
【问题讨论】:
标签: optimization matlab