MATLAB: Correlation pairs

cross correlation

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.

Best Answer

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]
>>