我正在寻找一种在更大的矩阵(任意维数)中找到矩阵(模式)的“好”方法。
例子:
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/