2012-01-16 6 views
1

값 당신은 당신이 CovMatrix(:,3)의 상관 값으로 구성된 셀 어레이와 CovMatrix(:,1)의 상관 관계 CovMatrix(:,2) 계산에 사용 된 데이터의 이름으로 끝날 것이다 다음 코드를 실행하면 :

clear all 
FieldName = {'Name1','Name2','Name3','Name4','Name5'}; 
Data={rand(12,1),rand(12,1),rand(12,1),rand(12,1),rand(12,1)}; 
DataCell = [FieldName;Data];%place in a structure - this is the same 
%structure that the data for the lakes will be placed in. 
DataStructure = struct(DataCell{:}); 
FieldName = fieldnames(DataStructure); 
Combinations = nchoosek (1:numel(FieldName),2); 
d1 = cell2mat(struct2cell(DataStructure)');%this will be the surface temperatures 
%use the combinations found in 'Combinations' to define which elements to 
%use in calculating the coherence. 
R = cell(1,size(Combinations,1));%pre-allocate the cell array 
Names1 = cell(1,size(Combinations,1)); 
for j = 1:size(Combinations,1); 
    [R{j},P{j}] = corrcoef([d1(:,[Combinations(j,1)]),d1(:,[Combinations(j,2)])]); 
    Names1{j} = ([FieldName([Combinations(j,1)],1),FieldName([Combinations(j,2)],1)]); 
end 
%only obtain a single value for the correlation and p-value 
for i = 1:size(Combinations,1); 
    R{1,i} = R{1,i}(1,2); 
    P{1,i} = P{1,i}(1,2); 
end 
R = R';P = P'; 
%COVARIANCE MATRIX 
CovMatrix=cell(size(Combinations,1),3);%pre-allocate memory 
for i=1:size(Combinations,1); 
    CovMatrix{i,3}=R{i,1}; 
    CovMatrix{i,1}=Names1{1,i}{1,1}; 
    CovMatrix{i,2}=Names1{1,i}{1,2}; 
end 

이것으로부터, 바람직하게는 상관 행렬의 형태로 값의 테이블을 생성 할 필요가 있습니다. 이것은 jeremytheadventurer.blogspot.com와 유사합니다. 이것이 MATLAB에서 가능한가?

+1

이전 [질문] (http://stackoverflow.com/questions/8851290/correlation-in-matlab)의 반복처럼 보입니다. 좋은 대답을 얻지 못한 이유는 아마도 질문이 명확하지 않고 코드 예제가 복잡해야하기 때문일 것입니다. – Kavka

+0

명확하지 않을 수 있습니다. 코드는 결과를 산출하는 예제 코드 일 뿐이며 코드 구조는 부적합합니다. 'CovMatrix'결과에서 링크에 표시된 표로 이동할 수 있는지 묻는 것입니다. 복잡한? – user1053544

답변

0

당신은 corrcoef 명령을 사용하여 한 번에 전체 데이터 세트의 상관 행렬을 계산할 수있다 : 당신이 상위 삼각형 부분을 무시하면

% d1 can be simply computed as 
d1_new = cell2mat(Data); 

% Make sure that d1_new is the same matrix as d1 
max(abs(d1(:)-d1_new(:))) 

% Compute correlation matrix of columns of data in d1_new in one shot 
CovMat = corrcoef(d1_new) 

% Make sure that entries in CovMat are equivalent to the third column of 
% CovMatrix, e.g. 
CovMat(1,2)-CovMatrix{1,3} 
CovMat(1,4)-CovMatrix{3,3} 
CovMat(3,4)-CovMatrix{8,3} 
CovMat(4,5)-CovMatrix{10,3} 

상관 행렬 CovMat 대칭이기 때문에, 이것은 필요한 결과를 포함하고 있습니다.

+0

그리고 0으로 원하면 다음을 할 수 있습니다 : tril (CovMat, 0) –