For loop for correlation

6 次查看(过去 30 天)
Anuradha
Anuradha 2013-12-17
回答: Jos (10584) 2013-12-17
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.

回答(1 个)

Jos (10584)
Jos (10584) 2013-12-17
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,:)

Community Treasure Hunt

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

Start Hunting!

Translated by