For loop for correlation
Show older comments
I have a large dataset (9x6925) where I need to find the corrcoef, keeping one column constant and testing it with the other columns. How can I do that using for loop.
Answers (1)
Jos (10584)
on 17 Dec 2013
Here is a method:
% create some data
A = 10*rand(10,4) ;
k0 = 1 ; % reference column
% add some columns for demo purposes
A(:,end+1) = 2*A(:,k0) ; % perfect correlation with
A(:,end+1) = 3*A(:,k0) + 2*rand(10,1) ; % some correlation
% find the correlations among columns
N = size(A,2) ;
CC = zeros(1,N) ; % pre-allocation to store the coefficients
for k = 1:N,% loop over all columns
tmp = corrcoef(A(:,k0),A(:,k)) ;
CC(k) = tmp(1,2) ;
end
disp(CC)
Note that CC(k0) is 1, per definition
You could also use corrcoef to get all correlations and select the ones you want
tmp = corrcoef(A) % A N-by-N array
CC = tmp(k0,:)
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!