http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683
MATLAB Central Newsreader  pattern matching among two matrix
Feed for thread: pattern matching among two matrix
enus
©19942017 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://nl.mathworks.com/images/membrane_icon.gif

Thu, 24 Nov 2011 18:48:08 +0000
pattern matching among two matrix
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683#859485
PANDIAN` NITHYANANDAM
hai matlab expert<br>
<br>
i would like to check a particular pattern of first matrix(2d) available in the second matrix (2d) or not.<br>
<br>
is it possible to check like that ; what is the function available for that.<br>
kindly help me regarding this.

Thu, 24 Nov 2011 20:01:09 +0000
Re: pattern matching among two matrix
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683#859494
Roger Stafford
"PANDIAN` NITHYANANDAM" <nithyanandamp@ssn.edulin> wrote in message <jam3h8$sj1$1@newscl01ah.mathworks.com>...<br>
> i would like to check a particular pattern of first matrix(2d) available in the second matrix (2d) or not.<br>
       <br>
To my knowledge there is no single function that performs such a test; you have to devise your own. The following is a bruteforce (and therefore slow) method of searching for an exact match using the 'all' function. Call A the first matrix and B the (larger) second matrix.<br>
<br>
[m,n] = size(A);<br>
[p,q] = size(B);<br>
b = false;<br>
i1 = 0;<br>
while i1 <= pm & ~b<br>
i1 = i1 + 1;<br>
i2 = 0;<br>
while i2 <= qn & ~b<br>
i2 = i2 + 1;<br>
b = all(all(A==B(11:i1+m1,i2:i2+n1)));<br>
end<br>
end<br>
<br>
At exit the logical variable 'b' will be true if an exact match was found, and false otherwise. The indices i1 and i2 at exit will indicate where the first matching position occurred, if any.<br>
<br>
Roger Stafford

Fri, 25 Nov 2011 04:29:46 +0000
Re: pattern matching among two matrix
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683#859515
ImageAnalyst
On Nov 24, 3:01 pm, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> "PANDIAN` NITHYANANDAM" <nithyanand...@ssn.edulin> wrote in message <jam3h8$sj...@newscl01ah.mathworks.com>...<br>
> > i would like to check a particular pattern of first matrix(2d) available in the second matrix (2d) or not.<br>
><br>
>        <br>
> To my knowledge there is no single function that performs such a test; you have to devise your own.<br>
><br>
> Roger Stafford<br>
<br>
<br>
What about the normalized cross correlation, normxcorr2(), in the<br>
Image Processing Toolbox?<br>
<br>
normxcorr2<br>
<br>
Normalized 2D crosscorrelation<br>
Syntax<br>
<br>
C = normxcorr2(template, A)<br>
Description<br>
<br>
C = normxcorr2(template, A) computes the normalized crosscorrelation<br>
of the matrices template and A. The matrix A must be larger than the<br>
matrix template for the normalization to be meaningful. The values of<br>
template cannot all be the same. The resulting matrix C contains the<br>
correlation coefficients, which can range in value from 1.0 to 1.0.<br>
<br>
Can't you just call that and look for 1.0 if you're looking for an<br>
exact match, or somewhat less if you're looking for an approximate<br>
match?<br>
ImageAnalyst

Fri, 25 Nov 2011 10:35:08 +0000
Re: pattern matching among two matrix
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683#859525
Roger Stafford
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <885f3bdcdb644b10935fbc18baab742e@i6g2000vbe.googlegroups.com>...<br>
> What about the normalized cross correlation, normxcorr2(), in the<br>
> Image Processing Toolbox?<br>
> ........<br>
> Can't you just call that and look for 1.0 if you're looking for an<br>
> exact match, or somewhat less if you're looking for an approximate<br>
> match?<br>
         <br>
I interpreted Pandian's "pattern ... available" as meaning equality of corresponding elements, which is what my code tested. Having a normalized cross correlation (Pearson coefficient) of unity does not imply such equality. As a counterexample let x = [1;2;6] and y = [3;5;13]. Then the Pearson coefficient between them is:<br>
<br>
((13)*(37)+(23)*(57)+(63)*(137)) / ...<br>
sqrt( ((13)^2+(23)^2+(63)^2)*((37)^2+(57)^2+(137)^2) ) =<br>
(8+2+18)/sqrt((4+1+9)*(16+4+36)) = 28/sqrt(14*56) = 28/28 = 1<br>
<br>
The vectors x and y therefore have a correlation of 1 and yet are certainly far from being equal, though they are linearly related: y=2*x+1.<br>
<br>
See for example<br>
<br>
<a href="http://en.wikipedia.org/wiki/Correlation_and_dependence">http://en.wikipedia.org/wiki/Correlation_and_dependence</a><br>
<br>
Roger Stafford

Fri, 25 Nov 2011 17:21:45 +0000
Re: pattern matching among two matrix
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683#859564
ImageAnalyst
I interpreted it as doing pattern recognition, like finding a small<br>
matrix that is located within a larger matrix. Like this example<br>
where it generates a big matrix, then gets a template from a certain<br>
place in the big matrix (so we know for a fact that the pattern does<br>
exist), and then finding where it it located:<br>
<br>
% Generate the big matrix<br>
bigMatrix = randi(99, [15 15])<br>
% Get a template that's known to exist in the big matrix.<br>
template = bigMatrix(3:5, 7:9)<br>
<br>
% Get the normalized cross correlation.<br>
nxc = normxcorr2(template, bigMatrix)<br>
<br>
% Crop it to the original size.<br>
nxc = nxc(2:16, 2:16)<br>
% Find the row and columns where it matches.<br>
[row col] = find(nxc >= 0.999)<br>
fprintf('Template found at row=%d, column = %d', row, col);

Fri, 25 Nov 2011 19:17:08 +0000
Re: pattern matching among two matrix
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/314683#859574
Roger Stafford
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <cb60d1d51a81418591b1ef0fefe5efc6@h42g2000yqd.googlegroups.com>...<br>
> .......<br>
> template = bigMatrix(3:5, 7:9)<br>
> .......<br>
         <br>
You have given the 'template' equality to a portion of 'bigMatrix' in stating that<br>
<br>
template = bigMatrix(3:5, 7:9)<br>
<br>
However the counterexample I mentioned shows that you can get a correlation of 1 at that relative location even where you might only have something like<br>
<br>
template = 10*bigMatrix(3:5, 7:9)100<br>
<br>
holding true there.<br>
<br>
It is a consequence of the Cauchy–Bunyakovsky–Schwarz inequality theorem that equality of the correlation to 1 or 1 is true if and only if the two matrix portions are linearly dependent. They do not have to be equal.<br>
<br>
Of course if the notion of pattern matching is satisfied by merely having linearly related patterns, then searching for a correlation of 1 would be a good method of detecting such a condition. It all hinges on the meaning to be ascribed to a "match" of two patterns.<br>
<br>
Roger Stafford