Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / October 2008



Tip: Looking for answers? Try searching our database.

help with simple 2-d autocorrelation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave Rowland - 24 Oct 2008 18:50 GMT
I am pretty inexperienced when it comes to matlab and could use a little help with a seemingly simple problem.  Basically I have a matrix of numbers and would like to see how well correlated the numbers are at any given spatial lag.  In other words the zero-order score would the matrix correlated with an exact copy (i.e it would be 1) then the copied matrix would be shifted at different spatial lags (because the data are 2-d this would involve moving the shifted copy up-down as well as left-right).  I would like the correlation scores to be stored as matrix.  This should, I think, mean that the size of the resulting matrix will be [2m-2, 2n-2] if the original matrix was [m,n].  The correlation score should be a simple pearson's correlation coefficient.  Thanks!
Bruno Luong - 24 Oct 2008 19:40 GMT
> I am pretty inexperienced when it comes to matlab and could use a little help with a seemingly simple problem.  Basically I have a matrix of numbers and would like to see how well correlated the numbers are at any given spatial lag.  In other words the zero-order score would the matrix correlated with an exact copy (i.e it would be 1) then the copied matrix would be shifted at different spatial lags (because the data are 2-d this would involve moving the shifted copy up-down as well as left-right).  I would like the correlation scores to be stored as matrix.  This should, I think, mean that the size of the resulting matrix will be [2m-2, 2n-2] if the original matrix was [m,n].  The correlation score should be a simple pearson's correlation coefficient.  Thanks!

You might take a look at function conv2 and work out from that.

Bruno
Dave Rowland - 25 Oct 2008 23:22 GMT
> I am pretty inexperienced when it comes to matlab and could use a little help with a seemingly simple problem.  Basically I have a matrix of numbers and would like to see how well correlated the numbers are at any given spatial lag.  In other words the zero-order score would the matrix correlated with an exact copy (i.e it would be 1) then the copied matrix would be shifted at different spatial lags (because the data are 2-d this would involve moving the shifted copy up-down as well as left-right).  I would like the correlation scores to be stored as matrix.  This should, I think, mean that the size of the resulting matrix will be [2m-2, 2n-2] if the original matrix was [m,n].  The correlation score should be a simple pearson's correlation coefficient.  Thanks!  

Thanks for the tip.  I am still a little stuck.  The signal processing toolbox has a function called xcorr2, which seems to be closer to what I need.  The xcorr2 function seems to compute the dot product at each lab, but that is only one part of what I need for the pearsons correlation stat.  It would be nice if I could modify the xcorr2 code, but of course I can't.  It seems like I really need to start from scratch.  It seems like I first need a bit of code that will systematically shift the copy matrix, second will compute the simple correlation stat at each shift, and third put the computed stat in a new matrix.  If you can see a good way to use conv2 or xcorr2 to help me out with this please let me know.  
Thanks!
Dave Rowland
Bruno Luong - 26 Oct 2008 08:13 GMT
> It seems like I first need a bit of code that will systematically shift the copy matrix, second will compute the simple correlation stat at each shift, and third put the computed stat in a new matrix.  

conv2 computes these three steps in one shot. All you need is to flip one the original matrix in both directions before passing it to one of the input arguments of conv2.

Bruno
Bruno Luong - 26 Oct 2008 08:26 GMT
Sorry, I reread your post and I don't think I understand what is stuck. Was is the dot product is the only varying term on the correlation when the image is shifted? So to me once the dot product is computed, the rest follows.

Bruno
Dave Rowland - 26 Oct 2008 17:55 GMT
> Sorry, I reread your post and I don't think I understand what is stuck. Was is the dot product is the only varying term on the correlation when the image is shifted? So to me once the dot product is computed, the rest follows.
>
> Bruno

Thanks again.  I was originally hopeful when I saw the conv2 and xcorr2 functions that you led me to, but as you recognized the problem is that for each shifted version of the matrix I need to know four things: 1) the dot product (provided), 2) the sum of all observations for overlapping portions in both the shifted and original matrix 3) the sums of squares for the overlapping portions in each and 4) the number of overlapping bins between them.  The fact that matlab has a builtin that does the dot product and creates a matrix of the right size is a good start, but I need to fill in the rest of the variables to compute the pearson coef.
Thanks!
Dave Rowland
Bruno Luong - 26 Oct 2008 18:24 GMT
The fact that matlab has a builtin that does the dot product and creates a matrix of the right size is a good start, but I need to fill in the rest of the variables to compute the pearson coef.

Oh no problem, if you want to compute the mean in corresponding sub-regions, why not using exactly xcorr2 (see below). Do similarly on the square the matrix for the second moment and variance. Combine this with shifted dot product of A and you are done.

%Compute mean on sub-region

A=round(10*rand(3,4))

Uno=ones(size(A));
% S=xcorr2(Uno,Uno);
% S=S(1:size(A,1),1:size(A,2))
S = (1:size(A,1))'*(1:size(A,2));

% Mean of sub region of A, started from upper-left
M1=xcorr2(A,Uno);
M1=M1(1:size(A,1),1:size(A,2))
M1=M1./S

% Mean of sub region of A, started from bottom-right
M2=xcorr2(Uno,A);
M2=M2(1:size(A,1),1:size(A,2))
M2=M2./S

% Bruno
Wolfgang Schwanghart - 26 Oct 2008 19:39 GMT
Hi Dave,

my first reply seemed not to be published. So, again. You might find a variogram useful to estimate spatial autocorrelation. You'll find a function on the FEX, that calculates this for irregularly scattered data.

http://www.mathworks.com/matlabcentral/fileexchange/20355

I have written a variogram function for grids, too. Just send me an email and I'll send it to you.

Best regards,
Wolfgang
Wolfgang Schwanghart - 26 Oct 2008 23:58 GMT
In case you have the image processing toolbox, have a look at
normxcorr2

HTH,
Wolfgang
Dave Rowland - 31 Oct 2008 17:51 GMT
> In case you have the image processing toolbox, have a look at
> normxcorr2
>
> HTH,
> Wolfgang

Thanks for your help, Bruno and Wolfgang.  I have something together that seems to work for my purposes.  Now I have a related question: I need to generate something my field calls "coherence".  "Coherence" is a correlation between a list of the value in each pixel and a corresponding list of the average value in the immediately surrounding pixels.  In other words, it is sort of like the first order spatial autocorrelation.  My hope was this value would fall out of the autocorrelation function that I just made, but that doesn't appear to be true.  At any rate, generating a list of the values in each pixel is trivial (even I can do it!), but I'm a little hung up on how to get the list of the average values.  Any help from the community would be great!
Dave Rowland
Dave Rowland - 01 Nov 2008 00:55 GMT
> > In case you have the image processing toolbox, have a look at
> > normxcorr2
[quoted text clipped - 4 lines]
> Thanks for your help, Bruno and Wolfgang.  I have something together that seems to work for my purposes.  Now I have a related question: I need to generate something my field calls "coherence".  "Coherence" is a correlation between a list of the value in each pixel and a corresponding list of the average value in the immediately surrounding pixels.  In other words, it is sort of like the first order spatial autocorrelation.  My hope was this value would fall out of the autocorrelation function that I just made, but that doesn't appear to be true.  At any rate, generating a list of the values in each pixel is trivial (even I can do it!), but I'm a little hung up on how to get the list of the average values.  Any help from the community would be great!
> Dave Rowland          

I think I figured it out.  Sorry no need for replies.  Thanks!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.