title: What is a fast way to compute column by column correlation in matlab
body: <p>I have two very large matrices (60x25000) and I'd like to compute the correlation between the columns only between the two matrices. For example:</p>
  
  <pre><code>corrVal(1) = corr(mat1(:,1), mat2(:,1);
  corrVal(2) = corr(mat1(:,2), mat2(:,2);
  ...
  corrVal(i) = corr(mat1(:,i), mat2(:,i);
  </code></pre>
  
  <p>For smaller matrices I can simply use:</p>
  
  <pre><code>   colCorr = diag( corr( mat1, mat2 ) );
  </code></pre>
  
  <p>but this doesn't work for very large matrices as I run out of memory. I've considered slicing up the matrices to compute the correlations and then combining the results but it seems like a waste to compute correlation between column combinations that I'm not actually interested.</p>
  
  <p>Is there a quick way to directly compute what I'm interested?</p>
  
  <p><strong>Edit</strong>: I've used a loop in the past but its just way to slow:</p>
  
  <pre><code>mat1 = rand(60,5000);
  mat2 = rand(60,5000);
  nCol = size(mat1,2);
  corrVal = zeros(nCol,1);
  
  tic;
  for i = 1:nCol
      corrVal(i) = corr(mat1(:,i), mat2(:,i));
  end
  toc; 
  </code></pre>
  
  <p>This takes ~1 second</p>
  
  <pre><code>tic;
  corrVal = diag(corr(mat1,mat2));
  toc;
  </code></pre>
  
  <p>This takes ~0.2 seconds</p>