Find matching small matrix in larger matrix

14 vues (au cours des 30 derniers jours)
Luis Rosety
Luis Rosety le 23 Juin 2022
Commenté : Luis Rosety le 23 Juin 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

Réponse acceptée

Michael
Michael le 23 Juin 2022
Modifié(e) : Michael le 23 Juin 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 commentaire
Luis Rosety
Luis Rosety le 23 Juin 2022
Thanks!
Even though I was convinced there was some magical function, your "brute force approach" solves my problem!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by