how do i compare two matrices with different sizes.so that i get to know when elements of smaller matrix are exactly matched to elements in larger matrix?

 Accepted Answer

"Dead ahead" solution -- it's undoubtedly doable via conv2 or the like and there may be something in the Image Processing Toolbox (I don't have) that is even better but the brute force route would be something like--
[M,N]=size(R); % the larger array
[m,n]=size(r); % and the smaller...
for i=1:M/m-(m-1)
for j=1:N/n-(n-1)
if all(r==R(i:i+m-1,j:j+n-1))
disp('found')
end
end
end
Above presumes M/m == fix(M/m) and ditto for N/n...
ERRATUM Correct limits on subset indices; had done a local sanity check test using a 2x2 and had hardcoded 'i+1' that missed in the generalization to arbitrary size.

More Answers (1)

doc ismember % maybe?
NB: if are floating point values you'll probably need to use an inexact comparison to ensure "close enough" is still seen as "equal".

1 Comment

There are only binary values in both matrices, what I want is that using for loop matlab will tell how many times both matrices are matched, if you got my point.

Sign in to comment.

Asked:

on 12 May 2015

Edited:

dpb
on 14 May 2015

Community Treasure Hunt

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

Start Hunting!