How to find a matrix inside a larger matrix?

12 vues (au cours des 30 derniers jours)
Jose Grimaldo
Jose Grimaldo le 28 Avr 2020
Im trying to find a smaller matrix inside a larger matrix using explicit loops. I know there is functions and probably easier to locate if there is matching matrix inside a larger matrix. I want to locate matrix M, inside matrix A. How can i set up the for loops since both matrix are different size?
A,6x6 matrix
A=[8 2 9 7 6 7;
9 5 4 1 7 0;
1 9 8 6 7 2;
5 4 1 0 3 0;
9 8 6 8 6 0;
4 1 0 5 4 1];
B, 3x3 matrix
M=[5 4 1; 9 8 6; 4 1 0];
[r,c]=size(A)
[row,col]=size(M)
  2 commentaires
jessupj
jessupj le 28 Avr 2020
Modifié(e) : jessupj le 28 Avr 2020
if you're beholden to using loops:
you can iterate only a portion of the rows, columnns that fit inside the larger matrix, and test whether the submatrix starting at the appropriate row,col (ie. upper left index) matches the desired matrix. if it matches, store the row,col
match_ul=[]
for rr = 1:(r-row+1)
for cc = 1:(c-col+1);
if isequal( A( rr:(rr+row-1), cc:(cc+col-1) ), M );
match_ul = [match_ul; [rr cc] ];
end
end
end
then you can iterate over the rows of this list to test:
k=1
A( match_ul(k,1)+(0:row-1), match_ul(k,2)+(0:col-1))
ans =
5 4 1
9 8 6
4 1 0
k=2
A( match_ul(k,1)+(0:row-1), match_ul(k,2)+(0:col-1))
ans =
5 4 1
9 8 6
4 1 0
Rik
Rik le 28 Avr 2020
Why do you insist on loops? Or is this a homework assignment?

Connectez-vous pour commenter.

Réponses (1)

SaiDileep Kola
SaiDileep Kola le 8 Mai 2020
Follow the link provided here for a similar query resolved

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by