Search code examples
matlabfor-loopcorrelationpearson

Corrcoef in Matlab is very slow


I have the following code:

for k = 1:256
            for t = 1:10000
                % R matrix
                buffer = corrcoef(matrixA(:,k),matrixB(:,t));
                correlation_matrix(k,t)  = buffer (2,1);
            end
        end

I calculate the pearson correlation of the columns of two matrices. This works fine for me and the results are correct. However the process seems to be very very slow. Does anyone have an idea how to accelerate the calculations here?


Solution

  • You can remove the loop entirely by using corr from the stats toolbox

    >> matrixA = randn(100, 256);
    >> matrixB = randn(100, 10000);
    >> size(corr(matrixA, matrixB))
    ans =
    
       256   10000