Find matching small matrix in larger matrix

10 views (last 30 days)
Luis Rosety on 23 Jun 2022
Commented: Luis Rosety on 23 Jun 2022
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
0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

Accepted Answer

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 CommentShow NoneHide None
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.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!