#include #include #include #include #include #include #include #include #include//for convert int to string #include using namespace std; void fdr_pvalue(double &q,int pN,vector &p_value,vector &signal_pvalue,int &R,double &threshold); void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) { int i; if(nrhs!=4) { mexErrMsgTxt("Four inputs are required."); } //The first input int Get_N1= mxGetN(prhs[0]); if(mxGetM(prhs[0])!=1 || Get_N1!=4) mexErrMsgTxt("Input argument 1 must be a row vector of size 4."); double *input1=mxGetPr(prhs[0]); for(i=0;i<3;i++) { if((input1[i]-(double((int)input1[i])))>0 || input1[i]<1) mexErrMsgTxt("The first three elements of input argument 1 must be positive integers."); } if(!(input1[3]<1 && input1[3]>0)) mexErrMsgTxt("The last element of input argument 1 must be greater than 0 and less than 1."); int LX=(int)input1[0];//dimension of the lattice on x-axis int LY=(int)input1[1];//dimension of the lattice on y-axis int LZ=(int)input1[2];//dimension of the lattice on z-axis double q=input1[3];// control fdr at level q int Num=LX*LY*LZ;//total voxels of the lattice on the image grid ////////////////////////////////// //The 2nd to 4th inputs char *file[3];// the directory of file, /*file[0]: location file for the brain region of interest (ROI) on the LX*LY*LZ image grid *file[1]: observed data (p-value) on the whole image grid including the brain part and the non-brain part *file[2]: output file of signals for the ROI */ int filelen[3];//the length of directory int status[3]; for(i=1;i Loca;//Location of the ROI ifstream ifs_loca(file[0]); double temp_loca; while(ifs_loca>>temp_loca) Loca.push_back(int(temp_loca)-1);// The entry location of C++ array starts from 0, but vectorized image lattice location starts from 1 int PN=Loca.size();// the number of voxels in the ROI vector pvalue_all;//observed data (p-value) on the whole image grid including the brain part and the non-brain part ifstream ifs_pvalue(file[1]);// double temp_pvalue; while(ifs_pvalue>>temp_pvalue) pvalue_all.push_back(temp_pvalue);// read in y_ori vector pvalue(PN);//the p-values of the ROI for(int pi=0;pi signals(PN);//The signals of the ROI,1=signal, 0=non-signal double threshold;//threshold in the BH procedure int R;//the number of signals (rejections) fdr_pvalue(q,PN,pvalue,signals,R,threshold); //output the signal file ofstream signal_file; signal_file.open(file[2]); for(i=0;i &p_value,vector &signal_pvalue,int &R,double &threshold) { vector sort_pvalue(p_value); sort(sort_pvalue.begin(),sort_pvalue.end()); for(R=pN; R>0 && sort_pvalue[R-1]*pN>R*q; R--); if(R!=0) { threshold=sort_pvalue[R-1]; int time=0;// for the tie pvalue problem for(int j=0; j