Find matching small matrix in larger matrix

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Michael
Michael il 23 Giu 2022
Modificato: Michael il 23 Giu 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 Commento
Luis Rosety
Luis Rosety il 23 Giu 2022
Thanks!
Even though I was convinced there was some magical function, your "brute force approach" solves my problem!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by