【问题标题】:How to find out consecutive sequence of numbers in two different vectors in MATLAB?如何在MATLAB中找出两个不同向量中的连续数字序列?
【发布时间】:2019-11-11 18:45:37
【问题描述】:

假设一个向量是 x= [-2 -2 -1 -1 -1 -2 -1 0 5 -1 0 5 -1 0] 和其他向量是 y=[ 2 3 4 5 -1 0 5 -1 0 5 -1]。这两个向量不必具有相同的长度。我想使用 MATLAB 找出两个向量中最长连续数的相似序列/模式?结果应该是两个向量中匹配模式的开始和结束索引。对于此示例:ix=[7 12] 和 iy=[5 10]。

【问题讨论】:

    标签: arrays matlab vector pattern-matching


    【解决方案1】:

    这需要图像处理工具箱和统计工具箱。它使用块大小的循环:

    x = [-2 -2 -1 -1 -1 -2 -1 0 5 -1 0 5 -1 0];
    y = [ 2 3 4 5 -1 0 5 -1 0 5 -1];
    for n = min(numel(x), numel(y)):-1:1; % try sizes in decreasing order
        x_sliding = reshape(im2col(x,[1 n],'sliding'),n,[]).'; % reshape needed for n=1
        y_sliding = reshape(im2col(y,[1 n],'sliding'),n,[]).'; % reshape needed for n=1
        [ind_x, ind_y] = find(pdist2(x_sliding, y_sliding) == 0);
        if ~isempty(ind_x)
            ix_start = ind_x;
            iy_start = ind_y;
            ix_end = ind_x+n-1;
            iy_end = ind_y+n-1;
            break
        end
    end
    

    解决方案(如果存在)在ix_startix_endiy_startiy_end 中给出。如果有多个最大可能大小的解,则生成所有解的索引。

    【讨论】:

    • 工作正常。如果连续序列中的所有数字与给定示例不同,此方法是否有效?
    • 您没有在解决方案中的任何地方使用“y”?
    • 请检查我的问题的小更新。
    • @erbal 对不起,我以为你想要最长的等值运行
    猜你喜欢
    • 2016-10-04
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多