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?

2 views (last 30 days)
i need assistance in my object detection project.

Accepted Answer

dpb
dpb on 13 May 2015
Edited: dpb on 14 May 2015
"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)

dpb
dpb on 12 May 2015
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
Shoaib Ejaz
Shoaib Ejaz on 12 May 2015
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.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!