File Exchange

image thumbnail

A faster way to computer a correlation matrix than corr.m

version 1.0.0.0 (1.3 KB) by David Groppe
A much faster way to compute a linear correlation matrix than MATLAB's built in corr.m function.

2 Downloads

Updated 08 Feb 2012

View License

A simple function that provides a much faster way to compute a correlation matrix than MATLAB's built in corr.m function.

Cite As

David Groppe (2020). A faster way to computer a correlation matrix than corr.m (https://www.mathworks.com/matlabcentral/fileexchange/34982-a-faster-way-to-computer-a-correlation-matrix-than-corr-m), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

bme marchus

not fast, even slower, dealing with i.e. 500*20000 matrix

rob welsh

@John D'Errico, Yes, I need to update from 2010a.

John D'Errico

@Rob. Maybe you need to use an up to date version of MATLAB. (2014a here.)

On a matrix of the same size you show...

>> A = rand(64,4628);
>> timeit(@() faster_corr_mtrx(A))
ans =
2.9281

>> timeit(@() corr(A))
ans =
0.48925

The built-in corr is 6 times as fast.

rob welsh

tic;
d4=corr(rs3);
toc;
Elapsed time is 1065.725646 seconds.

rob welsh

@John D'Errico, not sure what that that's about. It is much faster on the 64x4628 mtrx I'm working with.
tic;
%changed faster_corr_mtrx to fcm_1
%otherwise original function
d3=fcm_1(rs3);%64x4628
toc;
Elapsed time is 8.673760 seconds.
Matlab's corr fx is still running...

John D'Errico

Is it really faster? Whenever I see a claim like this, I tend to question things, rather than just buy what the over the top advertising claims.

>> A = randn(1000);

>> timeit(@() corr(A))
ans =
0.074609

>> timeit(@() faster_corr_mtrx(A))
ans =
0.20854

So in fact, this code was almost 3 (THREE!!!) times slower than corr on a 1000x1000 matrix.

For arrays with many more rows than columns, we see less of a difference. There are two parts to this code. First, it calls cov, then it uses nested loops to scale the covariances.

>> A = randn(10000,1000);
>> timeit(@() corr(A))
ans =
0.50081
>> timeit(@() faster_corr_mtrx(A))
ans =
0.62362

So when the covariance call itself dominates the problem, corr is only a little faster. On a matrix with 1000 columns, those nested loops take time.

Only when the matrix is a relatively tiny one might this code gain, since it does no error checking and it has no other features in it. After all, the built-in corr has MANY other capabilities and options. The time required to check for those options and potential errors probably costs something, but that makes corr a friendly code.

Lets try a problem with only 10 columns.

>> A = randn(10000,10);
>> timeit(@() corr(A))
ans =
0.00077739
>> timeit(@() faster_corr_mtrx(A))
ans =
0.0007733

No gain at all. So sorry, but I see no reason to use this code. I wanted to give it a non-minimum rating only because it had decent help. On a second glance, it lacks an H1 line, the thing that helps you use lookfor when you can't remember how to spell the name faster_corr_mtrx.

Does saving the user from typing the letters a and i really save that much? If it really did, then why not name it fstr_crr_mtrx ?

I'll give it 2 stars because it is near the holidays and I'm feeling generous.

rob welsh

Nice improvement over the corr fx. thanks
Tweak for 2x faster yet (I use it with two mtrx):
C=cov([data,data2]);
n_dim=length(C);
R=zeros(n_dim,n_dim);
sd=sqrt(diag(C));
for a=1:n_dim,
%for b=1:n_dim,
for b=a+1:n_dim,
R(a,b)=C(a,b)/(sd(a)*sd(b));
end
end

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!