Find matching small matrix in larger matrix

8 Ansichten (letzte 30 Tage)
Luis Rosety
Luis Rosety am 23 Jun. 2022
Kommentiert: Luis Rosety am 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

Akzeptierte Antwort

Michael
Michael am 23 Jun. 2022
Bearbeitet: Michael am 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 Kommentar
Luis Rosety
Luis Rosety am 23 Jun. 2022
Thanks!
Even though I was convinced there was some magical function, your "brute force approach" solves my problem!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by