【问题标题】:Table of correlation values相关值表
【发布时间】:2012-01-16 12:02:09
【问题描述】:

如果您运行以下代码,您最终会得到一个元胞数组,该元胞数组由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 中是否可行?

【问题讨论】:

  • 似乎是您之前的@​​987654322@ 的重复。您没有得到好的答案可能是因为您的问题不清楚,并且您的代码示例比应有的复杂。
  • 看不明白,代码只是一个示例代码,只是产生一个结果,代码的结构无关紧要。只是询问是否有可能从结果“CovMatrix”到链接中显示的表格。复杂吗?

标签: matlab correlation


【解决方案1】:

您可以使用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 是对称的,所以如果忽略上三角部分,这将包含所需的结果。

【讨论】:

  • 如果你想让它为零,你可以这样做:tril(CovMat,0)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
相关资源
最近更新 更多