-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpearsonCor.m
53 lines (45 loc) · 1.31 KB
/
pearsonCor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%calculate pearson correlation
%take cells of fra and array of mice names (must be the same length0
function [Allcorrelation,samemiceCAll,diffmiceCAll,FRAmice,coeff,coeff_prm]=pearsonCor(FRAs,Names)
clear coeff
for i=1:length(FRAs)
for t=1:length(FRAs)
if size(FRAs{i},2)==size(FRAs{t},2)
C = cov(FRAs{i},FRAs{t});
coeff(i,t) = C(1,2) / sqrt(C(1,1) * C(2,2));
FRa1=FRAs{i}(:);
FRa2=FRAs{t}(:);
FRa1Perm=FRa1(randperm(length(FRa1)));
FRa2Perm=FRa2(randperm(length(FRa2)));
Cp = cov(FRa1Perm,FRa2Perm);
coeff_prm(i,t) = Cp(1,2) / sqrt(Cp(1,1) * Cp(2,2));
else
coeff(i,t) = nan;
coeff_prm(i,t)= nan;
end
end
end
clear Utemp
U = triu(coeff,1);
Allcorrelation=U(find(U~=0));
% sort corelations by mice
if nargin<2
Names = ones(1,length(FRAs));
end
difmice_names=unique(Names);
diffmiceCAll=[];
samemiceCAll=[];
clear samemiceC
clear diffmiceC
for ll=1:length(difmice_names)
mouseinds=find(Names==difmice_names(ll));
mouseinds_no=find(Names>difmice_names(ll));
U=triu(coeff(mouseinds,mouseinds),1);
samemiceC{ll}=U(find(U~=0));
samemiceCAll=[samemiceCAll;samemiceC{ll}];
U=reshape(coeff(mouseinds,mouseinds_no),1,[]);
diffmiceC{ll}=U;
diffmiceCAll=[diffmiceCAll diffmiceC{ll}];
FRAmice{ll}=FRAs(mouseinds);
end
end