correlation pairs

14 visualizzazioni (ultimi 30 giorni)
Richard
Richard il 12 Gen 2012
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.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 12 Gen 2012
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 Commenti
Richard
Richard il 13 Gen 2012
many thanks, option 1 worked great.
Richard
Richard il 13 Gen 2012
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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by