correlation pairs

14 ビュー (過去 30 日間)
Richard
Richard 2012 年 1 月 12 日
The following code calculates the correlation between 2 vectors:
clear all
%generate fake data
LName={'Name1','Name2','Name3'};
Data={rand(12,1),rand(12,1),rand(12,1)};
%place in a structure
d = [LName;Data];
Data = struct(d{:});
%find the correlation
SNames=fieldnames(Data);
pairs = combnk (1:numel(SNames),2);
for i = 1 : size (pairs,1)
[R{i},P{i}] = corrcoef(Data.(SNames{pairs(i,1)}),Data.(SNames{pairs(i,2)}));
Correlation{i}=R{i}(1,2);
end
However, I want to find the correlation between all possible combinations. So, not just calculate the correlation between 2 elements but between 2 and then between 3... and so on. I can find the correlation between 3 elements by changing the line:
pairs = combnk (1:numel(SNames),3);
to
for i = 1:length(fieldnames(Data));
pairs.(SNames{i,1}) = combnk (1:numel(SNames),i);
end
But I want to adapt this to not just list the names from 'SNames' but to write which combinations i.e. SName1 v SName2.

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 1 月 12 日
I think that You not need doing this. I'm offering the following variant:
LName={'Name1','Name2','Name3'};
Dat={rand(12,1),rand(12,1),rand(12,1)};
d = [LName;Dat];
Data = struct(d{:});
d1 = cell2mat(struct2cell(Data)');
[R,P] = corrcoef(d1);
Correlation = [LName(nchoosek(1:size(R,1),2)) num2cell(nonzeros(tril(R,-1)))]
OR
R = corrcoef([Dat{:}]);
Correlation = [nchoosek(1:size(R,1),2) nonzeros(tril(R,-1))]
OR
Correlation = sparse(tril(R,-1))
ADD eg:
>> LName={'Name1','Name2','Name3' 'Name4'};
Dat={rand(12,1),rand(12,1),rand(12,1),rand(12,1)};
R = corrcoef([Dat{:}])
Correlation = [LName(nchoosek(1:size(R,1),2)) num2cell(nonzeros(tril(R,-1)))]
R =
1 0.62864 0.50105 -0.066267
0.62864 1 0.20848 -0.0042032
0.50105 0.20848 1 0.23383
-0.066267 -0.0042032 0.23383 1
Correlation =
'Name1' 'Name2' [ 0.62864]
'Name1' 'Name3' [ 0.50105]
'Name1' 'Name4' [ -0.066267]
'Name2' 'Name3' [ 0.20848]
'Name2' 'Name4' [-0.0042032]
'Name3' 'Name4' [ 0.23383]
>>
  2 件のコメント
Richard
Richard 2012 年 1 月 13 日
many thanks, option 1 worked great.
Richard
Richard 2012 年 1 月 13 日
When trying to adapt this code to work for more elements i.e. the combination of 3 or even four elements it doesn't work. It works fine when calculating the correlation between 3 elements i.e. 'Name1' 'Name2' 'Name3' etc but wont work when trying to calculate the correlation between four elements, why is this?
error:??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by