How do I find the submatrix of a matrix?

조회 수: 12 (최근 30일)
Hena Hamid
Hena Hamid 2016년 6월 2일
편집: giannit 2021년 8월 15일
I'm new to Matlab so bear with me.
If I have a 2D matrix A and a submatrix B of A. How can I check if B is a submatrix of A?
I basically want to search for matrix B in A.
An example with code would be appreciated :)

채택된 답변

Jos (10584)
Jos (10584) 2016년 6월 2일
Here is a simple for-loop that would work for 2D cases
A = magic(6)
B = A(2:3,4:5)
% engine
szA = size(A) ;
szB = size(B) ;
szS = szA - szB + 1
tf = false(szA) ;
for r = 1:szS(1)
for c = 1:szS(2)
tf(r,c) = isequal(A(r:r+szB(1)-1,c:c+szB(2)-1),B) ;
end
end
[rout,cout] = find(tf)
  댓글 수: 1
Sloke Shrestha
Sloke Shrestha 2019년 6월 14일
Hello,
Could you explain how this algorithm works?
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

giannit
giannit 2021년 8월 15일
편집: giannit 2021년 8월 15일
[R,P] = ismember(B,A)
if R contains all ones then B is a submatrix of A and P contains the indices of A where B is located, ie A(P) = B

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by