【问题标题】:How can I optimize this timeline-matching code in Matlab?如何在 Matlab 中优化此时间线匹配代码?
【发布时间】: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


    【解决方案1】:

    可以使用Matlab的intersect函数:

    c = intersect(A, B)
    

    【讨论】:

    • 啊哈!我知道一定有什么东西(我只是不知道相交)谢谢!
    • 如果你知道一个时间线是另一个时间线的子集,你也可以使用 ismember()
    【解决方案2】:

    你不能打电话给INTERSECT吗?

    commonTimeline = intersect(timeline1,timeline2);
    commonTimeline =
         0     1     4     5     9
    

    【讨论】:

      【解决方案3】:

      您需要使用从intersect 返回的索引。

      [~ ia ib] = intersect(timeline1, timeline2);
      recondata1 = data1(ia);
      recondata2 = data2(ib);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-13
        • 2013-05-01
        • 2011-04-09
        • 1970-01-01
        • 1970-01-01
        • 2019-12-14
        • 2011-11-30
        • 1970-01-01
        相关资源
        最近更新 更多