
%Input: EdgeMatrix_NBSCorrected -- NBS corrected results for MDD vs. HC
%Input: NetworkMatrix -- Correlation between FC and Symptom
%Input: StatOpt: Comes with NetworkMatrix in DPABINet

%Output: FinalCorrectedMatrix: Finally corrected matrix

%YAN Chao-Gan, 220422.


FDRQ=0.05; %FDR Q value.


CorrectedMask = true(size(NetworkMatrix)).*(EdgeMatrix_NBSCorrected~=0);  %Apply EdgeMatrix_NBSCorrected

TriMsk = tril(logical(CorrectedMask), -1);  %Get the lower triangle, because FC is symmetric


Vec=NetworkMatrix(TriMsk); %Get the edges we need to correct


StatOpt.TailedFlag=2; %Two tailed.
VecP=w_StatToP(Vec, StatOpt); %Convert the Stat values to p values

 
FDRMsk=y_FDR_Vector(VecP, FDRQ); %FDR Correction
Vec=Vec.*FDRMsk;

%Let's get back to the symmetric matrix as the input
MatTemp=zeros(size(TriMsk));
MatTemp(TriMsk)=Vec;
DiagVec=diag(MatTemp);
FinalCorrectedMatrix=MatTemp+MatTemp';
FinalCorrectedMatrix(logical(eye(size(NetworkMatrix))))=DiagVec;


