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,:)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by