matlab矩阵中求子矩阵的通用方法

Den*_*din 5 matlab matrix find

我正在寻找一种“好”方法来在更大的矩阵(任意维数)中找到矩阵(模式)。

例子:

total = rand(3,4,5);
sub = total(2:3,1:3,3:4);
Run Code Online (Sandbox Code Playgroud)

现在我希望这样的事情发生:

loc = matrixFind(total, sub)
Run Code Online (Sandbox Code Playgroud)

在这种情况下loc应该变成[2 1 3].

现在我只对找到一个点(如果存在)感兴趣,并不担心舍入问题。可以假设sub“适合” total


这是我如何在 3 维上做到这一点,但感觉有更好的方法:

total = rand(3,4,5);
sub = total(2:3,1:3,3:4);
loc = [];
for x = 1:size(total,1)-size(sub,1)+1
    for y = 1:size(total,2)-size(sub,2)+1
        for z = 1:size(total,3)-size(sub,3)+1
            block = total(x:x+size(sub,1)-1,y:y+size(sub,2)-1,z:z+size(sub,3)-1);
            if isequal(sub,block)
                loc = [x y z]
            end
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

我希望为任意数量的维度找到可行的解决方案。

ojd*_*jdo 4

这是低性能但(据说)任意维度的函数。它用于find创建潜在匹配位置的(线性)索引列表total,然后仅检查适当大小的子块是否total匹配sub

function loc = matrixFind(total, sub)
%matrixFind find position of array in another array

    % initialize result
    loc = [];

    % pre-check: do all elements of sub exist in total?
    elements_in_both = intersect(sub(:), total(:));
    if numel(elements_in_both) < numel(unique(sub))
        % if not, return nothing
        return
    end

    % select a pivot element
    % Improvement: use least common element in total for less iterations
    pivot_element = sub(1);

    % determine linear index of all occurences of pivot_elemnent in total
    starting_positions = find(total == pivot_element);

    % prepare cell arrays for variable length subscript vectors
    [subscripts, subscript_ranges] = deal(cell([1, ndims(total)]));


    for k = 1:length(starting_positions)
        % fill subscript vector for starting position
        [subscripts{:}] = ind2sub(size(total), starting_positions(k));

        % add offsets according to size of sub per dimension
        for m = 1:length(subscripts)
            subscript_ranges{m} = subscripts{m}:subscripts{m} + size(sub, m) - 1;
        end

        % is subblock of total equal to sub
        if isequal(total(subscript_ranges{:}), sub)
            loc = [loc; cell2mat(subscripts)]; %#ok<AGROW>
        end
    end
end
Run Code Online (Sandbox Code Playgroud)