I would like to include a loop in my script which finds the correlation of every possible combination of the data. This can be done manually by the following code:
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
[R,P] = corrcoef(Data.Name1,Data.Name2);
[R2,P2] = corrcoef(Data.Name1,Data.Name3);
[R3,P3] = corrcoef(Data.Name2,Data.Name3);
However, I would like to do this in a loop, I have started but have failed at the first hurdle. My attempted loop, which doesn't work is shown below:
SNames=fieldnames(Data);
for i=1:numel(SNames);
[R{i},P{i}] = corrcoef(Data.(SNames{i}),Data.(SNames{i+1}));
end
I'm struggling on knowing how to tell matlab to loop over a different combination of values with every iteration.
Any help provided would be much appreciated.
Try something like this: