⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 C++实现模式识别 C均值法 很容易看的懂的
💻 TXT
字号:
#include"stdio.h"
#include"math.h"

typedef struct {
float x;        /* 坐标 */
float y;        /* 坐标 */
int class_or_number;        /* 类别或矢量的个数 */
}mode;

/* 变量声明 */
int n,i,k,j;
int number[7];
mode Point_c[7]={{0,0,0},{1,0,0},{0,1,0},{4,4,0},{4,5,0},{5,4,0},{5,5,0}};;
mode Point_z[7]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
mode Point_temp[7]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
float d[7][7];

void init_z()                     /* 初始化类心 */
{
for(i=0;i<n;i++)
{
Point_temp[i].x=Point_c[number[i]-1].x;
Point_temp[i].y=Point_c[number[i]-1].y;
Point_temp[i].class_or_number=i+1;
Point_c[number[i]-1].class_or_number=i+1;
}
}

void print_mode_c()                       
{
printf("--------------------------\n\n");
printf("显示模式矢量:\n");
printf("--------------------------\n");
printf("序号	x坐标		y坐标        类别\n");
printf("--------------------------\n");
for(i=0;i<7;i++)
printf("%3d %8.2f        %8.2f   %8d\n",i+1,Point_c[i].x,Point_c[i].y,Point_c[i].class_or_number);
printf("--------------------------\n\n");
}

void print_new_z()                       
{
printf("--------------------------\n\n");
printf("显示新类心:\n");
printf("--------------------------\n");
printf("类别     x坐标         y坐标         模式矢量的个数\n");
printf("--------------------------\n");
for(i=0;i<n;i++)
printf("%3d %8.2f         %8.2f	 %8d\n",i+1,Point_z[i].x,Point_z[i].y,Point_z[i].class_or_number);
printf("--------------------------\n\n");
}

float d_caculate(int m,int n)        /* 计算距离d */
{
return sqrt((Point_temp[m].x-Point_c[n].x)*(Point_temp[m].x-Point_c[n].x)+(Point_temp[m].y-Point_c[n].y)*(Point_temp[m].y-Point_c[n].y));
}

void new_class_or_number()            /* 将距离d赋给数组 */
{
for(i=0;i<7;i++)
for(j=0;j<n;j++)           /* n为聚类中心个数 */
{
d[i][j]=d_caculate(j,i);
}
}

void change_z(int index,int i) /* 聚类 */
{
Point_z[index].x=Point_z[index].x+Point_c[i].x;
Point_z[index].y=Point_z[index].y+Point_c[i].y;
Point_z[index].class_or_number++;
}

void search_and_class()            /* 查找最小距离并划分类 */
{
int min_index;
float min;
for(i=0;i<7;i++)
{min=d[i][0];
for(j=0;j<n;j++)
  {
if(d[i][j]<=min)
    {
min=d[i][j];
min_index=j;
    }
   }
change_z(min_index,i);        /* 划分类 */
Point_c[i].class_or_number=min_index+1;
}
}

void newz_caculate()        /* 计算新类心Z */
{
for(i=0;i<n;i++)                /* n为聚类中心个数 */
{
Point_z[i].x=Point_z[i].x/Point_z[i].class_or_number;
Point_z[i].y=Point_z[i].y/Point_z[i].class_or_number;
}
}

bool comapreto_temp()         /* 与旧的类心比较 */
{
for(i=0;i<n;)          /* n为聚类中心个数 */
   {
if((Point_temp[i].x==Point_z[i].x)&&(Point_temp[i].y==Point_z[i].y))
i++;
else return true;
	}
return false;
}

void  z_to_temp()               /* 将新类心 z 赋制植给旧类心 temp */
{
for(i=0;i<n;i++)        /* n为聚类中心个数 */
{
Point_temp[i].x=Point_z[i].x;
Point_temp[i].y=Point_z[i].y;
Point_temp[i].class_or_number=Point_z[i].class_or_number;
}
}

void clear_z()            /* 将新类心刷新 */
{
for(i=0;i<n;i++)
{
Point_z[i].x=0;
Point_z[i].y=0;
Point_z[i].class_or_number=0;
}
print_new_z();
}

void main()
{
char c;
k=1;
printf("--------------------------\n\n");
printf("初始化模式矢量(坐标值)\n");
print_mode_c();
printf("--------------------------\n\n");
printf("输入选取的初始化聚类中心的个数和序号:\n");
printf("\n个数n=\t");
scanf("%d",&n);
printf("\n坐标序号number=\t\n");
for(i=0;i<n;i++)
{
scanf("%d",&number[i]);
}
printf("--------------------------\n\n");
init_z(); 
c=getchar();             /* 初始化类心 */
while(1)    
{
printf("--------------------------\n\n");
printf("     第%d次聚类\n\n",k);
printf("--------------------------\n\n");
print_mode_c();
new_class_or_number();           /* 将距离d赋给数组     */
search_and_class();        /* 查找最小距离并划分类 */
newz_caculate();			/* 计算新类心Z     */
print_new_z();
print_mode_c();

if(comapreto_temp()) 
{
printf("按下任意建继续。。。\n\n");
c=getchar();
printf("===============================\n\n");
z_to_temp();            /* 将新类心 z 赋制植给旧类心 temp */
clear_z();
k++;
}         
else break;
}
printf("\n分类结束\n");
}

⌨️ 快捷键说明

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