title: Calculating pair correlation for image
body: <p>I have a binary image, lets say 512x512px. I want to calculate pair correlation g(x). So far I'm doing it in as primitive as inefective way, line by line:</p>
  
  <pre><code>function Cr = pairCorr(image)
  
  domains = imread(image); % read image
  domains(domains&gt;0) = 1;  % make sure its binary by setting 1 to values &gt; 0
  size = length(domains(:, 1)); % image size
  
  for i=1:size
      line = domains(:, i); % take one line...
      for j=1:size % and for each distance...
          s = line(1:end-size+j);
          Cr(i, j) = mean(s); %...calculate Cr as mean  
      end
  end
  
  Cr = mean(Cr); % average all lines
  </code></pre>
  
  <p>Any idea how to do it a bit faster?
  Thanks!</p>