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)
Show older comments
i need assistance in my object detection project.
0 Comments
Accepted Answer
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.
0 Comments
More Answers (1)
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".
See Also
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!