"tlawren " <tlawren300@gmail.com> wrote in message <iee1d8$kp4$1@fred.mathworks.com>...
> Greetings all. I'm new to the problem I am working on, so I'm sorry if my question is not clear.
>
> I am trying to find the best matchup/alignment (correlation) of a small matrix within a larger matrix. (I've included an example below to better explain what I'm trying to do.) So far, I have been using xcorr2(A,B), but the matches are always thrown off. I looked at how xcorr2() calculates correlation coefficients and it appears that large values (products of large values) dominate the match. Can I use xcorr2() to solve this problem or do I need to use something else?
>
> Example: Given A and B below, I want to find the location of A within B. Clearly the 2's should align, but xcorr2() gives the best correlation as being the 2 in A on top of the 5 in B.
>
> A =
> 0 0 0
> 0 2 0
> 0 0 0
>
> B =
> 0 0 0 0 0 0
> 0 5 0 0 2 0
> 0 0 0 0 0 0
One way would be to use minimum absolute difference instead of the normalized xcorrelation and a floating window. Here's an example for your 1dimensional case above (it's 1 dimensional since we're only going to move A along B).
%Data
A = [
0 0 0
0 2 0
0 0 0];
B =[
0 0 0 0 0 0
0 5 0 0 2 0
0 0 0 0 0 0];
%Engine
idx_dim2 = 1;
ad_current = inf;
for ii = 1:size(B,2)size(A,2)+1
ad_new = sum(reshape(abs(B(:,ii:ii+size(A,2)1)A),[],1));
if ad_new < ad_current
ad_current = ad_new;
idx_dim2 = ii;
end
end
Bbest = B(:,idx_dim2:idx_dim2+size(A,2)1)
