资源简介
关于求熵、相对熵、互信息的C语言一般算法,好资源大家共享!
代码片段和文件信息
#include
#include
#define u 20
void main()
{
void entropy(float x[]float y[]int s);
void joint_entropy(float (*p)[u]int sint t);
void condition_entropy(float (*p)[u]float px[]float py[]int sint t);
void relative_entropy(float x[]float y[]int t);
void mutual_information(float (*p)[u]float px[]float py[]int sint t);
int ijmn;
float Pxy[u][u]px[u]={0.0}py[u]={0.0}k=0;
/*H_X=H(X)平均自信息;H_XY=H(XY)联合熵;H_XpY=H(X|Y)、H_YpX=H(Y|X)条件熵;
Pypx[i][j]=P(y[j]|x[i])条件概率;
H_XpY=H(Y/X)条件熵;Py[j]=P(y[j])收到y[j]的概率;
pxpy[i][j]=P(x[i]/y[j])条件概率;Pxy[i][j]=P(x[i]y[j])联合概率;D_pqD_qp相对熵;I_XY互信息*/
printf(“please input the dimension of ‘X‘ and ‘Y‘\n“);
scanf(“%d %d“&m&n);
printf(“Please input the marginal distribution probability:\nafter you input one number please click the ‘enter‘\n“);
for(i=0;i {
printf(“px[%d]=“i);
scanf(“%f“&px[i]);
}
for(i=0;i {
printf(“py[%d]=“i);
scanf(“%f“&py[i]);
}
/*判断输入X的概率是否正确,不正确则退出程序*/
for(i=0;i {if((px[i]<0)||(px[i]>1))
{printf(“Please input right value of probability\n“);
goto End_exe;
}
k+=px[i];
}
if((k-1)*(k-1)>0.001)
{printf(“Please input the right value of probability\n“);
goto End_exe;
}
printf(“input the joint probability:\n“);
for(i=0;i for(j=0;j { printf(“pxy[%d][%d]=“ij);
scanf(“%f“&Pxy[i][j]);
}
entropy(pxpyn);
joint_entropy(Pxymn);
condition_entropy(Pxypxpymn);
relative_entropy(pxpyn);
mutual_information(Pxypxpymn);
End_exe:;
}
void entropy(float x[]float y[]int s)
{
int i;
double H_X=0H_Y=0;
for(i=0;i H_X+=
- 上一篇:TIFF的C++库
- 下一篇:FCM_S聚类图像分割程序
评论
共有 条评论