相关值表

时间:2012-01-16 12:02:09

标签: matlab correlation

如果运行以下代码,最终会得到一个由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 个答案:

答案 0 :(得分: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是对称的,所以如果忽略上三角形部分,则包含所需的结果。