For loop for correlation
조회 수: 6 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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,:)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!