matlab - 在matlab矩阵中查找子矩阵的一般方法

标签 matlab matrix find

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

例子:

total = rand(3,4,5);
sub = total(2:3,1:3,3:4);

现在我希望它发生:

loc = matrixFind(total, sub)

在这种情况下,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

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

最佳答案

这是一个低性能但(据说)任意维度的函数。它使用 find 创建 total 中潜在匹配位置的(线性)索引列表,然后只检查 total 的适当大小的子 block 匹配 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

关于matlab - 在matlab矩阵中查找子矩阵的一般方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18831011/

相关文章:

matlab - 分组箱线图matlab

c - 从 C 中的文件填充矩阵

java - 具有动态变量(天)的排列矩阵,每天应该 "generate"一个 for 循环吗?

bash - 列出目录中具有特定扩展名的所有文件

c++ - 我应该使用哪个容器来快速找到一个元素,因为知道通过构造它已经被订购了?

python - wxPython:ListCtrl查找具有给定字符串的项目

linux - Linux [Ubuntu 16.04]-为Anaconda Python3安装MATLAB引擎

matlab - 避免在 MATLAB 中矩阵之间被零除

matlab - (MATLAB) 线性方程的所有整数正解

具有多个数组的 Numpy 元素加法