Find matching small matrix in larger matrix

8 views (last 30 days)
Hi,
I need to find if a binary logical matrix is somewhere in a larger binary matrix.
I am looking for specific patterns like:
pat = [0 0 1; 0 1 0; 0 0 0]
if it is somewhere in a larger matrix por example:
As you can see the patter is found with its center at r=137, c=810
I need a way to get the position (if there is) of these submatrix in the larger matrix.
Any help?
Thanks

Accepted Answer

Michael
Michael on 23 Jun 2022
Edited: Michael on 23 Jun 2022
An admittedly brute force approach, but Matlab doesn't have a built in method for finding patterns in an array/matrix (at least it didn't back in 2017).
[blm_rows, blm_cols] = size(binarylogicalmatrix);
pat = [0 0 1; 0 1 0; 0 0 0];
[pat_rows, pat_cols] = size(pat);
for i = 1:blm_rows-pat_rows+1
for j = 1:blm_cols-pat_cols+1
subblm = binarylogicalmatrix(i:i+pat_rows-1,j:j+pat_cols-1)
if subblm == pat
disp('Pattern Found!')
found_row = i;
found_col = j;
break
end
end
end
  1 Comment
Luis Rosety
Luis Rosety on 23 Jun 2022
Thanks!
Even though I was convinced there was some magical function, your "brute force approach" solves my problem!

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!