General method to find submatrix in matlab matrix

前端 未结 3 2026
臣服心动
臣服心动 2020-11-29 08:50

I am looking for a \'good\' way to find a matrix (pattern) in a larger matrix (arbitrary number of dimensions).

Example:

total = rand(3,4,5);
sub = t         


        
3条回答
  •  悲哀的现实
    2020-11-29 09:35

    For an arbitrary number of dimensions, you might try convn.

    C = convn(total,reshape(sub(end:-1:1),size(sub)),'valid'); % flip dimensions of sub to be correlation
    [~,indmax] = max(C(:));
    % thanks to Eitan T for the next line
    cc = cell(1,ndims(total)); [cc{:}] = ind2sub(size(C),indmax); subs = [cc{:}]
    

    Thanks to Eitan T for the suggestion to use comma-separated lists for a generalized ind2sub.

    Finally, you should test the result with isequal because this is not a normalized cross correlation, meaning that larger numbers in a local subregion will inflate the correlation value potentially giving false positives. If your total matrix is very inhomogeneous with regions of large values, you might need to search other maxima in C.

提交回复
热议问题