Be the first to rate this file! 123 downloads (last 30 days) File Size: 3.48 KB File ID: #22358

Fast Correlation between two vectors

by Francesco Pozzi

 

09 Dec 2008

No BSD License  

fastcorrelation is a fast and numerically stable algorithm for computing the correlation (in C)

Download Now | Watch this File

File Information
Description

% FASTCORRELATION computes the correlation between vectors x and y by
% making use of fastcorr.dll which is the C implementation of a fast and
% numerically stable algorithm.
%
% In order to generate fastcorr.dll, you need to run this instruction and
% compile the c file from the Matlab Command Window:
%
% mex fastcorr.c
%
%*************************************************************************%
%
% SLOWCORRELATION is the function fastcorr.c implemented in MATLAB. It can
% be used for generale consultation but, since it's VERY slow, it's NOT
% supposed to be used in real computations.
%
%*************************************************************************%
%
% EXAMPLE1. If you need to check the input vectors:
% N = 10000000;
% x = cumsum(randn(N, 1));
% y = cumsum(randn(N, 1));
% correlation = fastcorrelation(x, y);
%
%*************************************************************************%
%
% EXAMPLE2. If you DON'T need to check the input vectors:
% N = 10000000;
% x = cumsum(randn(N, 1));
% y = cumsum(randn(N, 1));
% correlation = fastcorr(x, y);
%
%*************************************************************************%
%
% EXAMPLE3. Check running time and accuracy of corrcoef and fastcorr
%
% format long
% for N = 50:50:100000
% x = cumsum(randn(N, 1));
% y = cumsum(randn(N, 1));
% tic, temp = corrcoef(x, y); correlation1(N / 50) = temp(2); runningtime1(N / 50) = toc; [N, toc]
% tic, correlation2(N / 50) = fastcorr(x, y); runningtime2(N / 50) = toc; [N, toc]
% disp(sprintf('\n\n\n'))
% end
% % Check running times (blue for corrcoef, magenta for fastcorr):
% figure
% plot(runningtime1, '.b');
% hold on;
% plot(runningtime2, '.m');
%
% % Check running times (positive means corrcoef > fastcorr, negative means fastcorr > corrcoef):
% figure
% plot(runningtime1 - runningtime2, '.b');
%
% % Compare accuracy of computation (differences are negligible):
% figure
% plot(correlation1 - correlation2, '.')

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Variance of Vector Elements

MATLAB release MATLAB 7 (R14)
Zip File Content  
Other Files fastcorr.c,
fastcorrelation.m,
Read ME first.txt,
slowcorrelation.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
algorithm Cristina McIntire 09 Dec 2008 13:22:46
vectors Cristina McIntire 09 Dec 2008 13:22:46
correlation Francesco Pozzi 09 Dec 2008 13:22:53
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com