mynormalize.c

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· C语言 代码 · 共 37 行

C
37
字号
/* normalize the cell array clpot, in jtree_C_inf_engine*/
/* 2 input, 2 output                                    */
/* clpot                                                */
/* C, the number of cells in clpot                      */
/*                                                      */
/* clpot                                                */
/* loglik                                               */

#include <math.h>
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    int     i, j, n, C;
    double  sum, sum1;
    double  *pr;
	mxArray *pTemp;
	
    plhs[0] = mxDuplicateArray(prhs[0]);
	C = mxGetNumberOfElements(prhs[0]);
	for(i=0; i<C; i++){
		sum = 0.0;
		pTemp = mxGetCell(plhs[0], i);
		n = mxGetNumberOfElements(pTemp);
		pr = mxGetPr(pTemp);
		for(j=0; j<n; j++){
			sum += pr[j];
		}
		if(sum == 0.0) sum1 = 1.0;
		else sum1 = sum;
		for(j=0; j<n; j++){
			pr[j] /= sum1;
		}
	}
   	plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
    *mxGetPr(plhs[1]) = log(sum);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?